表结构
AA BB CC DD
0 0 0 10
10 2 2 2
40 3 3 2
输出结果:
AA BB CC DD 结余
0 0 0 10 10
10 2 2 2 14 =(10+10-2-2-2)
40 3 3 2 46 =(14+40-3-3-2)
..............sql如何写出?可以加字段??
AA BB CC DD
0 0 0 10
10 2 2 2
40 3 3 2
输出结果:
AA BB CC DD 结余
0 0 0 10 10
10 2 2 2 14 =(10+10-2-2-2)
40 3 3 2 46 =(14+40-3-3-2)
..............sql如何写出?可以加字段??
(
select 1 r1, 0 AA,0 BB,0 CC,10 DD from dual
union all
select 2 r1,10 AA,2 BB,2 CC,2 DD from dual
union all
select 3 r1,40 AA,3 BB,3 CC,2 DD from dual
)
select r1,AA,BB,CC,DD,sum(case when r1=1 then AA+BB+CC+DD else AA-BB-CC-DD end) over(order by r1) SUM1
from t1
select 0 aa,0 bb,0 cc,10 dd from dual
union all
select 10 aa,2 bb,2 cc,2 dd from dual
union all
select 40 aa,3 bb,3 cc,2 dd from dual
)
select rownum,AA,BB,CC,DD,sum(case when rownum=1 then AA+BB+CC+DD else AA-BB-CC-DD end) over(order by rownum) SUM1
from temp
with tab as (
select 1 id, 0 aa, 0 bb, 0 cc,10 dd from dual union all
select 2 id, 10 aa, 2 bb, 2 cc, 2 dd from dual union all
select 3 id, 40 aa, 3 bb, 3 cc, 2 dd from dual)
SELECT id, aa, bb, cc, dd, SUM(decode(aa + bb + cc, 0, dd, aa - bb - cc - dd)) over(ORDER BY id) "结余"
FROM tab;