Cumulative counts in a query-column

G

Guest

I want to create a column in a query where cumulative values are created from
an other column.
For example:
Column1 Column2
1 1
1 2
4 6
etc.

Thank you for helping me. (if possible)
 
D

Dirk Goldgar

mda said:
I want to create a column in a query where cumulative values are
created from an other column.
For example:
Column1 Column2
1 1
1 2
4 6
etc.

Thank you for helping me. (if possible)

If there is a third column which is a unique key, then you can do this
with a subquery. Suppose you have example table "tblExample" with these
columns:

PK Amount
1 1
2 1
3 4
4 5
5 23

Then you can write a query like this:

SELECT
tblExample.PK,
tblExample.Amount,
(
SELECT Sum(T.Amount) FROM tblExample T
WHERE T.PK <= tblExample.PK
) AS RunningTotal
FROM tblExample
ORDER BY tblExample.PK;

That may not be exactly correct, as I haven't tested it.
 

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