我有一个表,其中包含 PatientId(Int)和 Date(日期数据类型)。
看起来确实像下面
patientId Date
101 01/01/2001
102 01/02/2001
103 01/03/2002
104 01/03/2004
105 01/03/2004
106 01/04/2004
我想要的结果会给我
Count Year
3 2004
因为它有最多的患者,而且我们有两年有相同数量的患者,那么我们应该显示这两年的患者数量。 谢谢。
最佳答案
使用YEAR
从 date
列中提取年份的函数。在 group by
中使用提取的年份来获取 Year 的 count
select TOP 1 year([Date]),count(1) as [Count]
from Yourtable
Group by year([Date])
Order by [Count] desc
另一种方法是使用 DATEPART
select TOP 1 Datepart(year,[Date]),count(1) as [Count]
from Yourtable
Group by Datepart(year,[Date])
Order by [Count] desc
关于sql - 如何找到关系的 COUNT 结果的 MAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34279954/