Hello Random Googler !!!
You landed here today... u r welcome.
Now consider the following output.
The 'interesting' column is the last one
Name | Date | Amount | Cumulative Sum |
Mike | 01-JUN-13 | 1000 | 1000 |
John | 01-JUN-13 | 1000 | 2000 |
Omar | 01-JUL-13 | 1000 | 3000 |
Shamim | 01-JUL-13 | 1000 | 4000 |
Abdul | 01-JUL-13 | 1000 | 5000 |
Jim | 01-AUG-13 | 1000 | 6000 |
ZeGuru | 05-SEP-13 | 1000 | 7000 |
Duli | 06-OCT-13 | 1000 | 8000 |
Another example
(grouped by date... sorry partitioned by date)
Name | Date | Amount | Cumulative Sum |
Mike | 01-JUN-13 | 1000 | 1000 |
John | 01-JUN-13 | 1000 | 2000 |
Omar | 01-JUL-13 | 1000 | 1000 |
Shamim | 01-JUL-13 | 1000 | 2000 |
Abdul | 01-JUL-13 | 1000 | 3000 |
Jim | 01-AUG-13 | 1000 | 1000 |
ZeGuru | 05-SEP-13 | 1000 | 1000 |
Duli | 06-OCT-13 | 1000 | 1000 |
The first one was achieved by;
SELECT name, date, amount, sum(amount) over(order by payment_id) cummulative_sum
FROM vw_payment
THE second was achieved by;
SELECT name, date, amount, sum(amount) over(partition by date, order by payment_id) cummulative_sum
FROM vw_payment
(the partition by clause was used to restart the running totals for each date)
do i need to say that payment_id is just the PK (incremental/serial integer) of the original table ?
By the way this works for both PostgreSQL and Oracle
Again u r welcome
No comments:
Post a Comment
Feel free to leave a comment