简体   繁体   English

如何在SQL中做最大计数部分?

[英]How to do the max count part in SQL?

I was told to Find out which occupation has the greatest number of patients with conditionID=MC8 I dk how to do the greatest part..... Here my code right now 我被告知要找出哪个职业具有conditionID = MC8的患者人数最多,我将如何做最大的事情.....我的代码现在在这里

SELECT occupation
FROM Patient
WHERE EXISTS                  
(SELECT PatientID FROM PatientMedcon
    Where conditionID=’MC8’)    
GROUP BY occupation
HAVNG count(occupation) = (Select max(occupation)
From Patient

You should approach these types of queries using regular joins and then add additional factors. 您应该使用常规联接来处理这些类型的查询,然后添加其他因素。 The following gets the count of patients for each occupation with that condition: 以下获取该条件下每个职业的患者计数:

SELECT occupation, COUNT(*)
FROM Patient p JOIN
     PatentMedcon pm
     ON p.PatientId = pm.PatientId and
        pm.conditionId = 'MC8'
GROUP BY occupation
ORDER BY COUNT(*) DESC;

If you want the top row, that depends on the database. 如果要第一行,则取决于数据库。 It might be select top 1 , limit 1 at the end, fetch first 1 rows only at the end, or even something else. 它可能是select top 1 ,在末尾limit 1fetch first 1 rows only在末尾fetch first 1 rows only ,甚至其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM