Home > database >  Running Subtotals in Microsoft Access SQL
Running Subtotals in Microsoft Access SQL

Time:09-21

I am using Microsoft Access 2016 and there is table where I want to count each occurrence of a value in a column grouping by another column. Below is sample data from the table:

ID Date Weekday Trip_ID Day Location
1 9/11/2022 Mon TI06091122 1 Florida
2 9/12/2022 Tue TI06091122 2 Travel Day
3 9/13/2022 Wed TI06091122 3 Georgia
4 9/14/2022 Thu TI06091122 4 South Carolina
5 9/15/2022 Fri TI06091122 5 North Carolina
6 9/16/2022 Sat TI06091122 6 Travel Day
7 9/17/2022 Sun TI06091122 7 Florida
8 9/18/2022 Mon TI06091822 1 Florida
9 9/19/2022 Tue TI06091822 2 Travel Day
10 9/20/2022 Wed TI06091822 3 Travel Day
11 9/21/2022 Thu TI06091822 4 Travel Day
12 9/22/2022 Fri TI06091822 5 Colorado
13 9/23/2022 Sat TI06091822 6 California
14 9/24/2022 Sun TI06091822 7 Florida

I want to group the data by Trip_ID and then do a rolling count for each Location. The result I am aiming for is this:

Trip_ID Location RunningCount
TI06091122 Florida 1
TI06091122 Travel Day 1
TI06091122 Georgia 1
TI06091122 South Carolina 1
TI06091122 North Carolina 1
TI06091122 Travel Day 2
TI06091122 Florida 2
TI06091822 Florida 1
TI06091822 Travel Day 1
TI06091822 Travel Day 2
TI06091822 Travel Day 3
TI06091822 Colorado 1
TI06091822 California 1
TI06091822 New Mexico 1
TI06091822 Florida 2

I am able to do a rolling total for the whole table by adding a temporary column equal to 1, and then using DSUM on that column.

DSUM("Temp_Col","Table", "ID<=" & ID) As RunningCount

I believe if I do some sort of subquery per Trip_ID that would do the DSUM I would get what I am looking for, but am unable to determine how to set it up.

Any help would be greatly appreciated. Thank you.

CodePudding user response:

You would need to include Trip_id and Location references in your DSum() criteria in order to get the count for each combination of those two fields. However, since you're actually after a count, use DCount() instead of "... adding a temporary column equal to 1, and then using DSUM on that column".

Here is a tested example with a DCount() expression for that derived column:

SELECT
    hp.Trip_ID,
    hp.Location,
    DCount(
        "*",
        "tblHuskypride",
        "Trip_ID='" & hp.Trip_id & "' AND Location='" & hp.Location & "' AND ID<=" & hp.ID
        ) AS RunningCount
FROM tblHuskypride AS hp
ORDER BY hp.Date;

(Notice I renamed my copy of the table to "tblHuskypride".)

Although that gives you what I think you want, I don't like all the quotes it requires. So I would prefer to use a correlated subquery as in the following example.

SELECT
    hp.Trip_ID,
    hp.Location,
    (
        SELECT Count(*)
        FROM tblHuskypride AS hp2
        WHERE
                hp2.Trip_ID=hp.Trip_ID 
            AND hp2.Location=hp.Location
            AND hp2.Date<=hp.Date
    ) AS RunningCount
FROM tblHuskypride AS hp
ORDER BY hp.Date;

Notice I chose to use the Date field instead of ID for the counting. If that's not satisfactory, swap ID for Date. Here is the output from that query tested with your sample data:

Trip_ID Location RunningCount
TI06091122 Florida 1
TI06091122 Travel Day 1
TI06091122 Georgia 1
TI06091122 South Carolina 1
TI06091122 North Carolina 1
TI06091122 Travel Day 2
TI06091122 Florida 2
TI06091822 Florida 1
TI06091822 Travel Day 1
TI06091822 Travel Day 2
TI06091822 Travel Day 3
TI06091822 Colorado 1
TI06091822 California 1
TI06091822 Florida 2
  • Related