SQL Running Total, sqlrunningtotal
What does Running Total mean?
Suppose we have a table: TRANS, indicating that we have some transaction records. One column is id, and the other is money (indicating the amount of the transaction design ), our demand is how to calculate the total number of transactions accumulated after each transaction. Example:
Id |
Money |
1 |
10 |
2 |
20 |
3 |
15 |
4 |
5 |
5 |
50 |
If the sum is calculated, the values are 10, 30, 45, 50, and 100 in sequence.
One method:
SELECT T1.ID, T1.MONEY, SUM (T2.MONEY) RUNNING_TOTAL from trans T1, TRANS T2
WHERE T1.ID> = T2.ID
Group by T1.ID, T1.MONEY
Order by T1.ID ASC
In ORACLE, you can also use OVER:
Select id, SUM (MONEY) OVER (order by id) RUNNINGT_TOTAL FROM TRANS
References:
Http://www.1keydata.com/ SQL /sql-running-totals.html
Http://www.dba-oracle.com/t_over_analytic_function.htm
Http://www.1keydata.com/ SQL /advanced.html