inst_no inst_lvl sup_inst_no dbal cbal
0000        1       0000     null null
0001        2       0000     null null
0002        2       0000     null null
000101      3       0001     200  null
000102      4       000101   200  100
000201      3       0002     100  50
实现的查询结果:
inst_no inst_lvl sup_inst_no dbal cbal
0000      1        0000      300  150
0001      2        0000      200  100    
0002      2        0000      100  50
000101    3        0001      200  100
000102    4        000101    200  100
000201    3        0002      100  50说明:如果一个节点已经存在值,就说明下级机构数据已经汇总到该机构,
就不能重复汇总了,如果没,则要汇总到上级机构!请各位大侠帮忙啊!

解决方案 »

  1.   

    SQL> select * from tablet;
     
    INST_NO      INST_LVL SUP_INST_NO       DBAL       CBAL
    ---------- ---------- ----------- ---------- ----------
    0000                1                        
    0001                2 0000                   
    0002                2 0000                   
    000101              3 0001                   
    000102              4 000101             200        100
    000201              3 0002               100         50
     
    6 rows selected
     
    SQL> 
    SQL> select inst_no,
      2         inst_lvl,
      3         sup_inst_no,
      4         (select sum(nvl(dbal, 0))
      5            from tablet b
      6           start with b.inst_no = a.inst_no
      7          connect by b.sup_inst_no = prior b.inst_no) dbal,
      8         (select sum(nvl(cbal, 0))
      9            from tablet b
     10           start with b.inst_no = a.inst_no
     11          connect by b.sup_inst_no = prior b.inst_no) cbal
     12    from tablet a;
     
    INST_NO      INST_LVL SUP_INST_NO       DBAL       CBAL
    ---------- ---------- ----------- ---------- ----------
    0000                1                    300        150
    0001                2 0000               200        100
    0002                2 0000               100         50
    000101              3 0001               200        100
    000102              4 000101             200        100
    000201              3 0002               100         50
     
    6 rows selected
     
    SQL> 
      

  2.   

    多练练手,了解一些oracle常用函数
      

  3.   


    --这个不就是你的结果吗?
    select inst_no,
           inst_lvl,
           sup_inst_no,
           (select sum(nvl(cbal, 0))
            from tablet b
            start with b.inst_no = a.inst_no
            connect by b.sup_inst_no = prior b.inst_no) cbal
    from tablet a;
      

  4.   

    哦,那在计算的时候加上connect_by_isleaf=1限定就可以了.
      

  5.   

    select inst_no,
             inst_lvl,
             sup_inst_no,
             (select sum(nvl(dbal, 0))
                from tablet b     
               where connect_by_isleaf=1
               start with b.inst_no = a.inst_no
              connect by b.sup_inst_no = prior b.inst_no) dbal,
             (select sum(nvl(cbal, 0))
                from tablet b       
               where connect_by_isleaf=1
               start with b.inst_no = a.inst_no
              connect by b.sup_inst_no = prior b.inst_no) cbal
        from tablet a;