views:

72

answers:

3

The following code returns a couple of numbers, identifying people who take part in more than three activities.

SELECT pnr
FROM Participates
GROUP BY pnr
HAVING count(activities)>3;

I want the answer to be the number of people who participate in more than three activities though, i.e. "4", instead of four unique numbers. What to do?

A: 

You will need a WHERE clause on the pnr field to uniquely identify one of your groupings:

SELECT COUNT(pnr)
FROM Participates
GROUP BY pnr
WHERE pnr = 'whatever'
HAVING COUNT(activities)>3

The order of my clauses might be wrong

David
A: 
Select Count(Distinct pnr)
From Participates
Having Count(activities) > 3
Erik Escobedo
There is no 'GROUP BY' here so the `Count(Distinct pnr)` would count the distinct pnr in the whole table and would return that as the result if the number of activities in the whole table happened to be more than 3.
Martin Smith
+2  A: 

Access supports derived tables.

SELECT COUNT(*) AS NumberOfParticipants FROM 
(
SELECT pnr
  FROM Participates
 GROUP BY pnr
HAVING count(activities)>3
) T
Martin Smith
Awesome, exactly what I was looking for. Thank you
Kremlan