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说明:如果一个节点已经存在值,就说明下级机构数据已经汇总到该机构,
就不能重复汇总了,如果没,则要汇总到上级机构!请各位大侠帮忙啊!
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说明:如果一个节点已经存在值,就说明下级机构数据已经汇总到该机构,
就不能重复汇总了,如果没,则要汇总到上级机构!请各位大侠帮忙啊!
解决方案 »
- 急问ORACLE数据库同步的方法步骤!谢谢!
- oracle9i sqlplus worksheet无法登录,没有找到 orancrypt9.dll
- 使用oracle sql developer定义包,报错误: PLS-00103: 出现符号 "CREATE"
- Oracle Developer 2000 怎样屏蔽报表列印功能?
- 关于nls_date_language会话
- 这个分段commit的语句写对了吗?
- 如何起动Oracle OraHome 80TNSListener服务 急急.....在线等候
- ado访问ORACLE7,检索字符型数据,都是问号?为什么?
- 查询sql咨询
- 关于Oracle数据库
- oracle数据库优化
- 求一SQL写法
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>
--这个不就是你的结果吗?
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;
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;