Count distinct entires (1 Viewer)

pickslides

Red Sails In The Sunset
Local time
Tomorrow, 08:09
Joined
Apr 29, 2008
Messages
76
Hi all,

Please see DB attached.

I wish to count how many students are in each subject by year level

Output for this table would be a crosstab (select query is fine)

Subject / Year Level .... 7 ..... 8
Math
...... 8 ....... 5
Sport ...... 5 ......... 3



Thanks, Q
 

Attachments

  • TEMP Distinct Count Query.accdb
    704 KB · Views: 421

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:09
Joined
May 7, 2009
Messages
19,242
see Query1
 

Attachments

  • TEMP Distinct Count Query.accdb
    704 KB · Views: 329

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:09
Joined
May 7, 2009
Messages
19,242
oopps, see Query3 instead.
 

Attachments

  • TEMP Distinct Count Query.accdb
    704 KB · Views: 416

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:09
Joined
May 7, 2009
Messages
19,242
crosstab is the simplest way.
but you can use a simple select query and you
will need to modify everytime there is new Year Level:

SELECT Query2.Subject, Count(IIf([year level]='7',1,Null)) AS 7, Count(IIf([year level]='8',1,Null)) AS 8
FROM Query2
GROUP BY Query2.Subject;
 

pickslides

Red Sails In The Sunset
Local time
Tomorrow, 08:09
Joined
Apr 29, 2008
Messages
76
So always pigging backing queries?

If I had my subjects named 7Math , 8Math, 7Sport, 8Sport and so on, then I could get distinct count with one select query?
 

jdraw

Super Moderator
Staff member
Local time
Today, 18:09
Joined
Jan 23, 2006
Messages
15,379
COUNT(DISTINCT column_name) is not supported in Microsoft Access databases
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:09
Joined
Feb 19, 2002
Messages
43,275
Select Subject, Semester, Count(*) as Students From tblRoster
Group by Subject, Semester;

That will give you a list of all subjects by year with a count. Add criteria if you want only a specific year.
 

June7

AWF VIP
Local time
Today, 14:09
Joined
Mar 9, 2014
Messages
5,470
The field name is Year Level, not Semester.

However, because there are multiple tests for a student in the same subject and year, student will be counted more than once for that subject and year. If you want to count each student only once for each subject and year, need nested subquery.

SELECT [Year Level], Subject, Count(*) AS StudentCount
FROM (SELECT DISTINCT [Student ID], [Year Level], Subject FROM Student_Results)
GROUP BY [Year Level], Subject;

Strongly advise not to use space in naming convention.
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:09
Joined
Feb 19, 2002
Messages
43,275
If you don't have a table that defines each subject/Year Level for a student, your schema is incorrect.
 

Users who are viewing this thread

Top Bottom