日期:2014-05-17  浏览次数:20768 次

求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 all
select 'B' , 'C' from  dual union all
select 'B' , 'D' from  dual union all
select 'D' , 'E' from  dual union all
select 'C' , 'F' from  dual union all
select 'M' , 'B' from  dual union all
select 'M' , 'L' from  dual union all
select 'M' , 'N' from  dual union all
select 'L' , 'Z' from  dual union all
select 'Z' , 'N' from  dual union all
select 'N' , 'S' from  dual 
)select distinct r,child1 from (
select t.*,connect_by_isleaf leaf,connect_by_root parent1 r from  t
start with t.parent1 in ('A','Z','M') 
connect by prior t.child1=t.parent1
) a where a.leaf=1
order 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 parent
order by parent
查询结果:
A    B
M    B,N,L
Z    N