cumulative sum in a query

R

rpt

I need to calculate a cumulative sum in a query. For example if the query has
a field with the following data:
1
2
3
4

then the result should be:
1
3
6
10

Could you help me?
 
M

Marshall Barton

rpt said:
I need to calculate a cumulative sum in a query. For example if the query has
a field with the following data:
1
2
3
4

then the result should be:
1
3
6
10


The need for this kind of thing is fairly rare. Since,
a) you should normally not display the results of a query
directly to users, b) this kind of calculation should be
done in a report instead of its query, and c) a data entry
form has very little need to display this kind of value.

Anyway, to answer the question:

SELECT T.field,
(SELECT Sum(X.field)
FROM table As X
WHERE X.field <= T.field
) As result
FROM table As T
 

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

Similar Threads

Cumulative value 7
Cumulative sum 2
Using FIND to highlight text (with dashes) 4
Cumulative counts in a query 2
SUM in a UNION query 2
Delete Qry Problem 3
Calculating a Cumulative value 6
Running Sum - Error 1

Top