原表:
id item
1 60,65
1 87,35
2 40,68,70
要得到的查询
id item
1 60
1 65
1 87
1 35
2 40
2 68
2 70
wildware(狂浪)的方法:
WITH A AS(SELECT 1 id,'60,65' item FROM dual
UNION ALL SELECT 1,'87,35' FROM dual
union all select 2,'40,68,70' from dual)
select a.id,substr(a.item,instr(','||a.item,',',1,b.rn),instr(a.item||',',',',1,b.rn)-instr(','||a.item,',',1,b.rn))item
from a,
(select * from
(SELECT DISTINCT id FROM A),
(SELECT ROWNUM rn FROM dual CONNECT BY ROWNUM<=(
select max(length(item)-length(replace(item,',')))+1 from a)))b
WHERE A.ID=b.ID
and length(a.item)-length(replace(a.item,',',''))+1>=b.rn
我自己的方法:
with tmp as
(
select 1 id, '60,65' item from dual
union all
select 1 id, '87,35' item from dual
union all
select 2 id, '40,68,70' item from dual
)
select distinct id,
substr(regexp_substr(',' || item, ',([^,]+)', 1, level), 2) as "item"
from tmp
connect by level <= length(regexp_replace(item, '[^,]', '')) + 1
order by id
其中,如果没用distinct,会多很多重复的记录,有待研究。