I'm trying to count the number of clients associated with each health clinic, and I want to show all clinics even if the number of clients is 0. My query works as expected until I throw in a WHERE clause.
Here's a stripped down description of the database tables and query to get to the essence of the problem.
clients table
client_id | health_clinic_id | accepted
----------------------------------------
1 1 1
2 2 NULL
3 1 1
options_health_clinics table
health_clinic_id | health_clinic_name
--------------------------------------
1 South Valley
2 North Valley
3 East Valley
4 West Valley
The following query does what I expect, i.e because of the RIGHT JOIN all health clinics are shown even if the number of clients is 0 for a health clinic.
SELECT
options_health_clinics.health_clinic_name,
COALESCE(COUNT(clients.health_clinic_id), 0) AS n_clients
FROM clients
RIGHT JOIN options_health_clinics ON options_health_clinics.health_clinic_id = clients.health_clinic_id
GROUP BY options_health_clinics.health_clinic_id
As soon as I throw in the WHERE clause (below) to filter out clients who have not accepted, only the health clinics with non-zero counts are shown.
WHERE clients.accepted = 1
Why is that and how can I fix it?
Thanks, Mitchell