Saturday, January 23, 2010

ORACLE-Cumulative Sum at the Same Line

You can sum the total of a measure in the same line for each section in your row data. For example:

select deptno,ename,sal,
sum(sal) over (partition by deptno order by sal,ename) CumDeptTot,
sum(sal) over (partition by deptno) SalByDept,
sum(sal) over (order by deptno, sal) CumTot,
sum(sal) over () TotSal
from emp
order by deptno, sal

and the result looks like this:

deptnoenamesalCumDeptTotSalByDeptCumTotTotSal
10John1300130034001300 11300
10Michael1100240034002400 11300
10Robert1000340034003400 11300
20Vicky1200120035504600 11300
20Sally1250245035505850 11300
20Harry1100355035506950 11300
30Pavel1400140043508350 11300
30Dennis1400280043509750 11300
30Barbie15504350435011300 11300

No comments:

Post a Comment