Running Sum

L

Lotto

I'vee seen alot on reports and running sums, and created a report with
a running sum, but I really need a query, as I have to use this data
elsewhere. Data looks like:

Cust PG Day Count
Amt Running Sum
50418 125 1
162 162
50418 125 7
234 496
50418 125 14
486 982


I know this is easy, and thanks in advance for your help.
 
M

Michel Walsh

You need a field which uniquely defines the ordering of the run, like a
date_time field, or something like that.
-------------------------------------
SELECT a.customer,
a.dateTimeField,
LAST(a.amount),
SUM(b.amount)

FROM tableName AS INNER JOIN tableName AS b
ON a.customer = b.customer
AND a.dateTimeField >= b.dateTimeField

GROUP BY a.customer, a.dateTimeField
----------------------------------


'a' and 'b' are aliases to your single table, to be compared to two fingers
running on a single list, but while one finger,'a', is stopped on a
{customer, dateTimeField}, the second finger, 'b', can only stops ON rows
where

a.customer = b.customer AND a.dateTimeField >= b.dateTimeField

in order to contribute to

SUM(b.amount)


You can select other fields from each group with the LAST aggregate:


SELECT a.customer,
a.dateTimeField,
LAST(a.amount),
LAST(a.somethingElse),
SUM(b.amount)
...


or by defining a new group:

SELECT a.customer,
a.accountType, ' <----
a.dateTimeField,
LAST(a.amount),
SUM(b.amount)

FROM tableName AS INNER JOIN tableName AS b
ON a.customer = b.customer
AND a.accountType=b.accountType ' <----
AND a.dateTimeField >= b.dateTimeField

GROUP BY a.customer,
a.accountType, ' <----
a.dateTimeField




Hoping it may help,
Vanderghast, Access MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top