Skip to content
Advertisement

Anyway to add a constraint when using SQL count

SELECT count(diabetesstatus is null) as atleast3, patientcountry
      
FROM database
where PJMEntryTime between '2020-08-24' and '2020-08-25'

and diabetesstatus is null and >= 3

group by patiencountry

I want to to use count to include a criteria where patient doesn’t have diabetetes, and then using the query to

show country that has at least 3 patients that doesn’t have diabetes

How shoudl I be doing this? How can I include a constraint when using count?

yeah I think this should work, basically my logic is not every patient is diabetic. I want to put my query that is grouped by country, so I can show every country that have at least 3 patients that aren’t diabetic.

Advertisement

Answer

I think you want a having clause:

SELECT patientcountry, count(*)
FROM database
WHERE PJMEntryTime between '2020-08-24' and '2020-08-25' AND
      diabetesstatus is null
GROUP BY patiencountry
HAVING COUNT(*) >= 3;
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement