Cumulative SUM in a query (SQL access)

Viewed 49

Using MS access SQL I have a query (actually a UNION made of multiple queries) and need a cumulative sum (actually a statement of account which items are in chronological order).

How do I get a cumulative sum?

Since they are duplicates by date I have to add a new ID, however, SQL in MS access does not seem to have ROW_ID or similar.

1 Answers

So, we need to sort donation data into chronological order across multiple tables with duplicates. First combine all the tables of donators in one query which sets up the simplest syntax. Then to put things in order we need to have an order for the duplicate dates. The dataset has two natural ways to sort duplicate dates including the donator and the amount. For instance, we could decide that after the date bigger donations come first, If the rule is complicated enough we abstract it to a code module and into public function and include it in the query so that we can sort by it:

'Sorted Donations:'

SELECT  (BestDonator(q.donator)) as BestDonator, *
FROM tblCountries as q
UNION SELECT  (BestDonator(j.donator)) as BestDonator, *
FROM tblIndividuals as j
ORDER BY EvDate Asc, Amount DESC , BestDonator DESC;

Public Function BestDonator(donator As String) As Long
BestDonator = Len(donator) 'longer names are better :)'
End Function

with sorted donations we have settled on an order for the duplicate dates and have combined both individual donations and country donations, so now we can calculate the running sum directly using either dsum or a subquery. There is no need to calculate row id. The tricky part is getting the syntax correct. I ended up abstracting the running sum calculation to a function and omitting BestDonator because I couldn't easily paste together this query in the query designer and I ran out of time to bug fix

enter image description here

Public Function RunningSum(EvDate As Date, Amount As Currency)
RunningSum = DSum("Amount", "Sorted Donations", "(EvDate < #" & [EvDate] & "#) OR (EvDate = #" & [EvDate] & "# AND Amount >= " & [Amount] & ")")
End Function

Carefully note the OR in the Dsum part of the RunningSum calculation. This is the tricky part to summing the right amounts.

'output

-------------------------------------------------------------------------------------
|      donator       |       EvDate       |       Amount       |     RunningSum     |
-------------------------------------------------------------------------------------
| Reiny              |          1/10/2020 |                321 | 321                |
-------------------------------------------------------------------------------------
| Czechia            |           3/1/2020 |               7455 | 7776               |
-------------------------------------------------------------------------------------
| Germany            |          3/18/2020 |               4222 | 11998              |
-------------------------------------------------------------------------------------
| Jim                |          3/18/2020 |                222 | 12220              |
-------------------------------------------------------------------------------------
| Australien         |          4/15/2020 |              13423 | 25643              |
-------------------------------------------------------------------------------------
| Mike               |          5/31/2020 |                345 | 25988              |
-------------------------------------------------------------------------------------
| Portugal           |           6/6/2020 |               8755 | 34743              |
-------------------------------------------------------------------------------------
| Slovakia           |          8/31/2020 |               3455 | 38198              |
-------------------------------------------------------------------------------------
| Steve              |           9/6/2020 |                875 | 39073              |
-------------------------------------------------------------------------------------
| Japan              |         10/10/2020 |               5234 | 44307              |
-------------------------------------------------------------------------------------
| John               |         10/11/2020 |                465 | 44772              |
-------------------------------------------------------------------------------------
| Slowenia           |         11/11/2020 |               4665 | 49437              |
-------------------------------------------------------------------------------------
| Spain              |         11/22/2020 |               7677 | 57114              |
-------------------------------------------------------------------------------------
| Austria            |         11/22/2020 |               3221 | 60335              |
-------------------------------------------------------------------------------------
| Bill               |         11/22/2020 |                767 | 61102              |
-------------------------------------------------------------------------------------
| Bert               |          12/1/2020 |                755 | 61857              |
-------------------------------------------------------------------------------------
| Hungaria           |         12/24/2020 |               9996 | 71853              |
-------------------------------------------------------------------------------------
Related