3,496
社区成员




with tab as
(
select '001001001' code, 100 money from dual union all
select '001001002',200 from dual union all
select '001003',200 from dual
)
select distinct *
from (
select code, sum(money) from tab group by code
union all
select substr(code,1,6) code, sum(money) from tab group by substr(code,1,6)
union all
select substr(code,1,3) code, sum(money) from tab group by substr(code,1,3)
)
order by length(code),code
CODE SUM(MONEY)
------------ ----------
001 500
001001 300
001003 200
001001001 100
001001002 200
--或者
with tab as
(
select '001001001' code, 100 money from dual union all
select '001001002',200 from dual
)
select code, sum(money) from tab group by code
union all
select substr(code,1,6) code, sum(money) from tab group by substr(code,1,6)
union all
select substr(code,1,3) code, sum(money) from tab group by substr(code,1,3)
order by code
-----------------------------------------------
code sum(money)
001 300
001001 300
001001001 100
001001002 200
with tab as
(
select '001' A, '001' B, '001' C, 100 money from dual union all
select '001','001','002',200 from dual
)
--select * from tab
select A || B || C code, sum(money) amt from tab group by A || B || C
union all
select A || B code, sum(money) amt from tab group by A || B
union all
select A code, sum(money) amt from tab group by A
order by code
with tab as
(
select '001' A, '001' B, '001' C, 100 money from dual union all
select '001','001','002',200 from dual
)
--select * from tab
select A || B || C sid, sum(money) money from tab group by A || B || C
union all
select A || B sid, sum(money) from tab group by A || B
union all
select A sid, sum(money) from tab group by A
-----------------------------
sid money
001001001 100
001001002 200
001001 300
001 300
-- 要数据!