日期:2014-05-17 浏览次数:20921 次
with tb1(A ,B ,C) as(--原表 select 'a,b,c' ,'1' ,2 from dual union all select 'b,da,a' ,'t' ,3 from dual ) , tb2 (newA) as(--将原表字段A合并(newA) 效果'a,b,c,b,da,a' select wm_concat(A) from tb1) , tb3 (newaa) as(--将tb2中newA进行拆分(newaa),并去重复('a','b','c','d','da') select distinct newaa from(select regexp_substr(newA,'[^,]+', 1,rownum) newaa from tb2 connect by rownum<=lengthb(regexp_replace(newA,'[^,]+', ''))+1)) --tb1和tb2做连接,连接条件是在tb3.newaa在tb1.A能找到 select tb3.newaa,tb1.B,tb1.C from tb3,tb1 where instr(tb1.A,tb3.newaa)>0 order by tb1.B,tb1.C
------解决方案--------------------
with tt as (
select 'a,b,c' as type, '1' as t1, '2' as t2 from dual union all
select 'b,da,a' as type, 't' as t1, '3' as t2 from dual
)
select * from (
select regexp_substr(type,'[^,]+',1,(rownum-nvl((select length(replace(wm_concat(type),',','')) from (select rownum as id,tt.* from tt) b where b.id<a.id),0))
) as type,t1,t2 from (select rownum as id,tt.* from tt) a
connect by (rownum-nvl((select length(replace(wm_concat(type),',','')) from (select rownum as id,tt.* from tt) b where b.id<a.id),0))
<=length(replace(type,',',''))
) where type is not null;
------解决方案--------------------