简体   繁体   English

SQL通过MAX选择前1名

[英]SQL Selecting Top 1 with MAX

In a parking citation database I am trying to select the state that has the most citations. 在一个停车引用数据库中,我试图选择引用次数最多的状态。

SELECT State, COUNT(Citation) as MostViolations
FROM dbo.ParkingCitations
GROUP BY State
ORDER BY COUNT(Citation) DESC

The syntax above will give me the top state in the first row, but I'm not sure how I got about selecting just that distinct state only? 上面的语法将使我在第一行中处于顶部状态,但是我不确定如何只选择那个不同的状态? (Should I be using a subquery with TOP 1 somehow?) (我是否应该以某种方式在TOP 1使用子查询?)

I think TOP 1 does what you want: 我认为TOP 1可以满足您的要求:

SELECT TOP 1 State, COUNT(Citation) as MostViolations
FROM dbo.ParkingCitations
GROUP BY State
ORDER BY COUNT(Citation) DESC;

If you want all when there are ties, then use top 1 with ties . 如果想要在平局时全部赢,则将top 1 with ties使用。

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

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