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

一个父节点子节点统计的问题
比如我的表 另一张表
parentid cid cid count
  -1 1 1 100 
  1 2 2 200 
  -1 3 3 300 
  3 4 4 400 
  -1 5 5 500 
  1 6 6 600 
然后我想统计出 就是 这个样子:  
cid count
 1 900
 3 700
 5 500

谢谢各位了

------解决方案--------------------
900? 500? 怎么看着不大对劲呢
------解决方案--------------------
with DataTable as

select -1 as parentid,1 as cid,100 as count from dual union
select 1 as parentid,2 as cid,200 as count from dual union
select -1 as parentid,3 as cid,300 as count from dual union
select 3 as parentid,4 as cid,400 as count from dual union
select -1 as parentid,5 as cid,500 as count from dual union
select 1 as parentid,6 as cid,600 as count from dual
)
select a.cid,sum(a.count) from (
select t.cid as cid ,t.count as count from DataTable t where t.parentid=-1
union
select t.parentid as cid,t.count as count from DataTable t start with parentid <>-1 connect by parentid = prior cid
)a group by a.cid order by cid;