select COMMUNITY_AREA_NAME, count(COMMUNITY_AREA_NAME) as Number_Of_Schools
from CHICAGO_PUBLIC_SCHOOLS group by COMMUNITY_AREA_NAME order by 2 desc
我编写了上面的sql查询以获取结果集,该结果集说明了拥有最多学校的社区名称,结果就像这样 但是如何只显示前10个列呢?我使用“限额10”。但是它显示了一些错误。
select Top 10 COMMUNITY_AREA_NAME, count(COMMUNITY_AREA_NAME) as Number_Of_Schools
from CHICAGO_PUBLIC_SCHOOLS group by COMMUNITY_AREA_NAME order by 2 desc
Limit 10;
错误:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'Limit'.
答案 0 :(得分:5)
对于您的情况,只需放置TOP 10
即可。
select TOP 10 COMMUNITY_AREA_NAME, count(COMMUNITY_AREA_NAME) as Number_Of_Schools
from CHICAGO_PUBLIC_SCHOOLS
group by COMMUNITY_AREA_NAME
order by 2 desc
SQL Server不直接支持 LIMIT
,如果您使用的是SQLServer 2012+
,则可以使用OFFSET
和FETCH
,它们的工作方式类似于LIMIT
。您可以在这里Here
对于旧版本的SQL Server,可以使用ROW_NUMBER()
来实现相同目的。