Reset Running Total
517303Jun 8 2006 — edited Jun 14 2006Is there any way to reset the values of running totals within SQL ?
Example:
select A, Order_date, Val, SUM(Val) OVER (PARTITION BY a ORDER by Order_date) as "RTOT" from test1;
A|| Order_date Val RTOT
-- ------------------ --- ---
1 01-Jan-2005 100 100
1 02-Jan-2005 300 400
1 07-Jan-2005 200 600
1 10-Jan-2005 500 1100
1 15-Jan-2005 200 1300
1 16-Jan-2005 100 1400
2 03-Jan-2005 200 200
2 05-Jan-2005 500 700
2 13-Jan-2005 200 900
2 23-Jan-2005 300 1200
What I want to reset Running Total to 0 every time it => 600 for the record and start from 0 for the running total calculation for all subsequent records in the partition, so the result should be:
A Order_date Val RTOT
- ------------------ --- ------------------
1 01-Jan-2005 100 100
1 02-Jan-2005 300 400
1 07-Jan-2005 200 0 (because the running total =600)
1 10-Jan-2005 500 500
1 15-Jan-2005 200 0 (because the running total =700)
1 16-Jan-2005 100 100
2 03-Jan-2005 200 200
2 05-Jan-2005 500 0 (because the running total =700)
2 13-Jan-2005 200 200
2 23-Jan-2005 300 500
Any ideas how it can be done in SQL in 10.2?
Message was edited by:
user514300