请教一个向上逐级汇总的问题(sql语句如何写)

happyct 2012-12-01 11:54:01
数据如下:

对象名称 ID PARENT_ID 数量
总分类 123 0
分类1 4 123
分类11 5 123
A 0 5 100
B 0 5 100
C 0 5 200
分类12 6 123
A 0 6 100
B 0 6 200

分类节点都没有数量的,只有分类节点下面的产品有数量。
我现在的目的是,汇总每一个分类节点的明细产品数量,汇总后效果如下:

对象名称 ID PARENT_ID 数量
总分类 123 0 700
分类1 4 123 700
分类11 5 123 400
A 0 5 100
B 0 5 100
C 0 5 200
分类12 6 123 300
A 0 6 100
B 0 6 200

请支招,谢谢!
...全文
1647 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
mandyocean 2013-02-14
  • 打赏
  • 举报
回复
select * from emp;
happyct 2013-02-04
  • 打赏
  • 举报
回复
非常感谢兄台
ssqtjffcu 2012-12-01
  • 打赏
  • 举报
回复
with t as(
 select '总分类' "对象名称",'123' id,'b' parent_id,null 数量 from dual
 union all
 select '分类1','4','123',null from dual
 union all
 select '分类11','5','123',null from dual
 union all
 select 'A','0','5',100 from dual
 union all
 select 'B','0','5',100 from dual
 union all
 select 'C','0','5',200 from dual
 union all
 select '分类12','6','123',NULL from dual
 union all
 select 'A','0','6',100 from dual
 union all
 select 'B','0','6',200 from dual
 )
 select t.对象名称,t.id,t.parent_id,
        (select sum(数量) 
           from t t1
          start with 对象名称 = t.对象名称
                 and t1.parent_id = t.parent_id
         connect by prior id = parent_id) s
   from t;
对象名称 ID  PARENT_ID          S
-------- --- --------- ----------
总分类   123 b                700
分类1    4   123       
分类11   5   123              400
A        0   5                100
B        0   5                100
C        0   5                200
分类12   6   123              300
A        0   6                100
B        0   6                200
 
9 rows selected

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧