当前位置: 代码迷 >> Oracle开发 >> 求SQL解决方案
  详细解决方案

求SQL解决方案

热度:22   发布时间:2016-04-24 07:28:36.0
求SQL
有一个表结构:
PARENT CHILD
A B
B C
B D
D E
C F
M B
M L
M N
L Z
Z N
N S

怎样可以得到A\Z\M的叶子节点?
结果如:
ROOT LEAF
A F
A E
Z S
M F
M E
M S


谢谢~~~

------解决方案--------------------
或者这样
SQL code
with t as  (select 'A' parent1, 'B' child1 from  dual union allselect 'B' , 'C' from  dual union allselect 'B' , 'D' from  dual union allselect 'D' , 'E' from  dual union allselect 'C' , 'F' from  dual union allselect 'M' , 'B' from  dual union allselect 'M' , 'L' from  dual union allselect 'M' , 'N' from  dual union allselect 'L' , 'Z' from  dual union allselect 'Z' , 'N' from  dual union allselect 'N' , 'S' from  dual )select distinct r,child1 from (select t.*,connect_by_isleaf leaf,connect_by_root parent1 r from  tstart with t.parent1 in ('A','Z','M') connect by prior t.child1=t.parent1) a where a.leaf=1order by 1
------解决方案--------------------
SQL code
CREATE TABLE A (    PARENT VARCHAR2(10),       CHILD  VARCHAR2(10));统计sql:select        parent,wm_concat(CHILD) from A t where t.parent in ('A','Z','M')group by parentorder by parent查询结果:A    BM    B,N,LZ    N
  相关解决方案