SQL Running Total, sqlrunningtotal

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.