Get Simulated Cumulative Sum (Running Total) in Oracle

Posted By  Nimish Garg On 05 Mar 2011 05:03:19
emailbookmarkadd commentsprint
No of Views:749
Bookmarked:0 times
Votes:0 times

Introduction

In this tip,i will explain how to get the running total in oracle.

select 
    ename, 
    dname, 
    sal, 
    sum(sal) over (partition by dname) dept_wise_total,
    sum(sal) over (partition by dname order by dname, ename) deptwise_running_total, 
    sum(sal) over () total,
    sum(sal) over (order by dname, ename) running_total
from 
    scott.emp e, 
    scott.dept d
where 
    e.deptno=d.deptno
order by 
    dname, ename

Simulated Version

select 
    ename, 
    dname, 
    sal, 
    sum(sal) over (partition by dname order by dname, ename) deptwise_running_total,
    (select sum(sal) from scott.emp b where b.deptno=e.deptno and b.ename <= e.ename) sim_dept_runtotal,
    sum(sal) over (order by dname, ename) running_total,
    (select sum(sal) from scott.emp b where b.ename <= e.ename) sim_running_total
from 
    scott.emp e, 
    scott.dept d
where 
    e.deptno=d.deptno
order by 
    dname, ename

Output 

Image Loading

thank you for reading.

Sign Up to vote for this article
Other popular Tips/Tricks
Comments
By:KashifDate Of Posted:4/5/2011 3:11:47 AM
First is easy
I think the first Query is easy to understand.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top