当前位置: 代码迷 >> Oracle技术 >> oracle 计算节点上的子节点之和
  详细解决方案

oracle 计算节点上的子节点之和

热度:89   发布时间:2016-04-24 08:18:53.0
oracle 计算节点下的子节点之和
假如有表结构:
id pid
1   0
2   1
3   1
4   3
5   1
6   2

我要计算 ID=1 他的子节点就有(2,3,4,5,6)也就是5个子节点 一次类推
ID,节点数量
1,5
2,1
3,1
4,0
5,0
5,0

就是求每个叶子的宽度   求高手指点  小弟没分儿 求大哥们帮助
------解决方案--------------------

with t as (
select '1' id,'0' pid from dual
union all
select '2' id,'1' pid from dual
union all
select '3' id,'1' pid from dual
union all
select '4' id,'3' pid from dual
union all
select '5' id,'1' pid from dual
union all
select '6' id,'2' pid from dual
)
select t.id,
       (select count(1)
          from t tt
         start with tt.pid = t.id
        connect by prior tt.id = tt.pid)
  from t

------解决方案--------------------
为什么有两个5?我觉得2楼是对的。
  相关解决方案