首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

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

2012-12-29 
oracle 计算节点下的子节点之和假如有表结构:id pid102131435162我要计算 ID1 他的子节点就有(2,3,4,5,6)

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楼是对的。

热点排行