How to add running total column to a query

JGravesNBS

Registered User.
Local time
Today, 01:58
Joined
Apr 5, 2014
Messages
58
I have the following query:

SELECT Count(tblEmployee.id) AS [Count], tbllocal.ID AS LID, tbllocal.local_cd AS LCD, tbllocal.local_name AS LDE
FROM tblEmployee LEFT JOIN tbllocal ON tblEmployee.local_id = tbllocal.ID
GROUP BY tbllocal.ID, tbllocal.local_cd, tbllocal.local_name
ORDER BY tbllocal.local_name;

It displays the following:

PHP:
Count   LID LCD LDE
2719 
1       16  105	Local 158
1216    1   17  Local 17
528     5   17  Local 17
1       2   463 Local 463
1       13  841 Local 841

How do I add a 5th column Run Count that shows running total of the Count column?

Run Count
2719
2720
3936
4464
4465
4466
 
This has been solved many times in the forum. Search for "running total". Essentially you build another query off of this one and calculate your [Run Count] field with a DSUM looking back into this query.

One note, your ordering of that list isn't distinct enough. When you do the DSUM you will probably need to also order by LID (might even need to add LCD in there.
 
personally, I wouldn't bother. It's generally easier to find another way. It's hard, because running totals are diametrically opposed to the database concept.
 

Users who are viewing this thread

Back
Top Bottom