Share via

count distinct

Anonymous
2013-12-31T16:48:05+00:00

I am very new to MS Access and I am having trouble counting distinct records.  Each person in my database is tracked by their SSN.  Every time that a person is enrolled in a class, a row in the databse is populated with their SSN (SSN), the class provider (ORG), and the date of enrollment (ENROLLDATE). A person may enroll in many classes, so their SSN is entered multiple times, followed by different enrollment dates.  How do I count each enrolled person only once (by their SSN)?  Ideally, I want the query to return a list of the class providers with a count of every person that has enrolled at least one time.  For example:

Organization A enrolled 5 people (5 SSNs)

Organization B enrolled 10 people (10 SSNs)

Please help!!

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

13 answers

Sort by: Most helpful
  1. Anonymous
    2013-12-31T22:24:03+00:00

    Excellent, thank you so much! If I wanted to add another field, such as quarter, how would I add that? The question is: per organization, how many unduplicated people (SSNs) enrolled in a class each quarter?  The result would look like:

    Q1          Q2       Q3

    Organization A          5              7          6

    Organization B          9             12        2

    I really appreciate your help!

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2013-12-31T17:57:13+00:00

    Count the rows per Org returned by a subquery which returns a distinct set of Org/SSN values, e.g.

    SELECT Org, COUNT(*) AS StudentCount

    FROM (SELECT DISTINCT Org, SSN

               FROM Enrolments) AS DistinctStudents

    GROUP BY Org;

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2014-01-02T16:21:19+00:00

    It looks like Ken just forgot the Quarter in the Group By.

    GROUP BY Org**, Quarter;**

    Any field that doesn't have an aggregate function (SUM, COUNT, MAX...) needs to be included in the Group By.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-01-02T15:55:55+00:00

    Hi Ken,

    I have tried it a few times, but I get this error message:

    "You tried to execute a query that does not include a sepcified expression 'Quarter' as part of an aggregate function."

    Do you have any thoughts?

    Thank you!!

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2013-12-31T23:50:32+00:00

    You'd start by including the quarter in the result table:

    SELECT Org, Quarter, COUNT(*) AS StudentCount

    FROM (SELECT DISTINCT Org, DATEPART("q",EnrollDate) As Quarter, SSN

               FROM Enrolments) AS DistinctStudents

    GROUP BY Org;

    Then either convert it to a crosstab query, or use a report with a multi-column subreport to give the columnar layout.

    Was this answer helpful?

    0 comments No comments