我有这个查询,我需要获得数字和城市的最大值
怎么做?
select city,count(id) as number
from men
group by city
order by number desc
提前谢谢
答案 0 :(得分:1)
简单。添加TOP
clause以将返回的行数限制为1.请注意,在select
语句中,top子句的括号是可选的,其中行数是常量。如果您使用除常量之外的任何内容,则需要使用括号和SQL Server 2005+。但是,具有常量行数且没有括号的top
子句也适用于2000。
select top 1 city,count(id) as number
from men
group by city
order by number desc
答案 1 :(得分:1)
select top(1) city,count(id) as number
from men
group by city
order by number desc
答案 2 :(得分:1)
您的查询似乎没问题。
只需添加前1即可获得第一个结果:
select top 1 city,count(id) as number from men group by city order by number desc