数据如下:对象名称  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请支招,谢谢!

解决方案 »

  1.   

    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