|
|
顺道将我在 http://www.itpub.net/thread-1775413-1-1.html 的解法贴过来- with u as(
- select 111 a, 222 b from dual
- union
- select 111, 333 from dual
- union
- select 333, 444 from dual
- union
- select 555, 666 from dual
- union
- select 555, 777 from dual
- union
- select 888, 666 from dual
- ),
- t as (select a,b from u union select b,a from u),
- s as (select a,b, level lvl, sys_connect_by_path(a,',') con_str from t connect by nocycle prior a =b),
- r as (select regexp_substr(con_str,'\d{3}') root, max(con_str) max_con_str, max(lvl) max_lvl from s group by regexp_substr(con_str,'\d{3}')),
- q as (select x.root, x.max_con_str, x.max_lvl from r x, r y where x.root<>y.root and instr(x.max_con_str, y.max_con_str)>0 group by x.root, x.max_con_str, x.max_lvl),
- p as (select x.root, x.max_con_str, x.max_lvl from q x, q y where x.root<>y.root and instr(x.max_con_str,y.root)>0),
- o as (select root, p1.rn, regexp_substr(max_con_str,'\d{3}',1,p1.rn) ele, max_lvl from p, (select rownum rn from dual connect by rownum<=(select max(max_lvl) from p)) p1 where p1.rn<=max_lvl),
- n as (select root, listagg(ele,',')within group(order by ele) recombine from o group by root)
- select recombine from n group by recombine
- /
- RECOMBINE
- 555,666,777,888
- 111,222,333,444
复制代码 |
|