oracle 经典语句集合

1、一列转多行

方法一:

select a.id,
  substr(','||a.name||',',instr(','||a.name,',',1,b.rn)+1,
    instr(a.name||',',',',1,b.rn)-instr(','||a.name,',',1,b.rn))name
from tt a,
(select rownum rn from dual
  connect by rownum<10)b
where length(a.name)-length(replace(a.name,','))+1>=b.rn
order by id,b.rn

 

方法二:

SELECT DISTINCT ID,REGEXP_SUBSTR(room, '[^,]+', 1, LEVEL, 'i') AS STR 
  FROM ttt
CONNECT BY LEVEL <= LENGTH(room) - LENGTH(REGEXP_REPLACE(room, ',', ''))+1;

 

 

 

你可能感兴趣的:(oracle)