Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accounts + balance

Status
Not open for further replies.

vlitim

Programmer
Sep 2, 2000
393
GB
I have a table which has trasactions in it which have an amount.

ie

id amount
1 50000
2 400
3 -378
4 344

I need to add a new column showing what the balance was at every transaction point but am stuck on how to do it in an efficient way, can anyone help please?

Cheers
tim
 
This is called "running total":
Code:
select id, amount, 
	(select sum(amount) from myTable B where id< A.id) as balance
from myTable A
order by A.id
If you change < to <=, balance column will contain value after transaction.

Similar examples: Other methods:
------
heisenbug: A bug that disappears or alters its behavior when one attempts to probe or isolate it
schroedinbug: A bug that doesn't appear until someone reads source code and realizes it never should have worked, at which point the program promptly stops working for everybody until fixed.

[ba
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top