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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running total in a query 1

Status
Not open for further replies.

rewdee

Programmer
Aug 17, 2001
295
US
If I have a table like:
ID SomeField
1 5
2 1
3 2
etc.

How would I write (if possible) a query that keeps a running total as:
ID SomeField Calculated_Running_Total
1 5 5
2 1 6
3 2 8
etc.

Any help would be greatly appreciated.

Thanks,
Rewdee
 
One way:
SELECT A.ID, A.SomeField, (SELECT Sum(B.SomeField) FROM yourTable B WHERE B.ID<=A.ID) AS Calculated_Running_Total
FROM yourTable A;
Another way:
SELECT A.ID, A.SomeField, Sum(B.SomeField) AS Calculated_Running_Total
FROM yourTable A INNER JOIN yourTable B ON A.ID>=B.ID
GROUP BY A.ID, A.SomeField;

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top