|
Tip of the Week Tip for Week of May 23, 2005
Getting Cumulative Sum
This tip comes from
Chacravarthi Ramasami, technical consultant at DSRC Pvt. Ltd., in Chennai, Tamilnadu, India.
This code gives a cumulative sum for a column. This example uses the SAL column in the standard EMP table.
SQL> select b.sal,sum(a.sal) as cum_sal
2 from emp a,emp b
3 where a.rowid <= b.rowid
4 group by b.rowid,b.sal
5 /
SAL CUM_SAL
--------- ---------
800 800
1600 2400
1250 3650
2975 6625
1250 7875
2850 10725
2450 13175
3000 16175
5000 21175
1500 22675
1100 23775
SAL CUM_SAL
--------- ---------
950 24725
3000 27725
1300 29025
14 rows selected.
|