So, I was trying to count the number of times a patient went to the hospital for each of the available 10 patients (including those who does not went at all, for no good reason), but only wanting to print the ones whose visit count is higher than the average of all recorded visit counts. How can I do this, when I can't compare any count values in the WHERE and HAVING clauses, and can't precalculate the average first the compare to it later). Please help
My guess is you're looking for something like :
SELECT
patientid,
count(*)
FROM visits
GROUP BY patientid
HAVING count(*) >
(SELECT AVG(ct) FROM
(SELECT count(*) ct FROM visits GROUP BY patientid) x
)
Visits might have to be upgraded to a left join between patient (on the left) and visits if there are patients who have never visited and you want their zeros to influence the average
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.