DSum/DCount - Number of Unique records

jack555

Member
Local time
Today, 15:15
Joined
Apr 20, 2020
Messages
93
I would like to generate the number of category occurrences in a particular month for the below example table. I think it can be done via DSUM/DCOUNT but couldn't able to achieve. Please help.

CategoryItemNameMonth
FruitsApple2021-08
FruitsOrange2021-08
VegChilli2021-08
VegOnion2021-08
SnackBiscuit2021-08
SnackChips2021-08
FruitsApple2021-08
FruitsOrange2021-08
VegChilli2021-08
VegOnion2021-08
VegChilli2021-08
VegOnion2021-08
FruitsApple2021-09
FruitsOrange2021-09
VegChilli2021-09
VegOnion2021-09
SnackBiscuit2021-09
SnackChips2021-09
SnackBiscuit2021-09
SnackChips2021-09

Desired Result

The month is represented in two columns, but even in the same colum is ok.

Category2021-082021-09
Fruits
2​
1​
Veg
3​
1​
Snack
1​
2​

Or

CategoryMonthCount
Fruits2021-08
2​
Veg2021-08
3​
Snack2021-08
1​
Fruits2021-09
1​
Veg2021-09
1​
Snack2021-09
2​

My Access expertise - beginner
 
Last edited:
Have you tried a Totals query or a Crosstab query?
 
Have you tried a Totals query or a Crosstab query?
I tried crosstab and total query without success. I could able to produce the total number of records, not the occurrences of category. I might be doing something wrong.
 
It's not really clear what constitutes an 'occurrence'. Why does Snack show 2 for 2021-08?

Consider:

Query1:
SELECT Data.Category, Data.ItemName, Data.Month, Count("*") AS Cnt
FROM Data
GROUP BY Data.Category, Data.ItemName, Data.Month;

Query2:
SELECT DISTINCT Query1.Category, Query1.Month, Query1.Cnt FROM Query1;
 
Last edited:
It's not really clear what constitutes an 'occurrence'. Why does Snack show 2 for 2021-08?

Consider:

Query1:
SELECT Data.Category, Data.ItemName, Data.Month, Count("*") AS Cnt
FROM Data
GROUP BY Data.Category, Data.ItemName, Data.Month;

Query2:
SELECT DISTINCT Query1.Category, Query1.Month, Query1.Cnt FROM Query1;
Yes, this solves my problem. Thank you very much.

PS: updated the question to correct wrong info about "snack"
 
I tried that before posting answer. Does not produce desired output.
 
You can do this with a single query:

SELECT Data.Category, Data.Month, Count("*") AS Cnt
FROM Data
GROUP BY Data.Category, Data.Month;

Do NOT include columns you want to summarize away.

June7 was answering a different question which was what your words said. I answered the question using your suggested answer.
This counts all the records of Data.Category. I wish Category should be counted only once for each group of Data.ItemName.

Suggestion by @June7 works fine. out of curiosity, I am thinking about how to make this work in a single query.
 
Can nest the queries for one SQL statement.

SELECT DISTINCT Query1.Category, Query1.Month, Query1.Cnt FROM
(SELECT Data.Category, Data.ItemName, Data.Month, Count("*") AS Cnt
FROM Data
GROUP BY Data.Category, Data.ItemName, Data.Month) AS
Query1;
 
Can nest the queries for one SQL statement.

SELECT DISTINCT Query1.Category, Query1.Month, Query1.Cnt FROM
(SELECT Data.Category, Data.ItemName, Data.Month, Count("*") AS Cnt
FROM Data
GROUP BY Data.Category, Data.ItemName, Data.Month) AS
Query1;
Exactly. my problem solved.
 

Users who are viewing this thread

Back
Top Bottom