我有以下查询,用于检索每个国家/地区的用户数量; 6 R4 m2 g/ Q3 ~SELECT C.CountryID AS CountryID, C.CountryName AS Country, Count(FirstName) AS OriginFROM Users AS UINNER JOIN Country AS C ON C.CountryID = U.CountryOfOrginGROUP BY CASE C.CountryName, C.CountryID我需要的是获得前10名,然后将所有其他用户汇总到一行。我知道如何获得前10名,但我坚持把剩下的人排成一排。有没有简单的方法可以做到这一点?9 p# Z# q* E) e3 x3 @. ^
例如,如果上述查询返回17条记录,则显示前十名,其他7个国家/地区的用户总数应显示在第11行。在银行11,countryid为0,countryname 8 A/ z* q0 }" P) D. b* \- z) N. @Others # F: V/ C% }1 c9 [! {0 X' ]谢谢你的帮助!3 u. }' H+ J S6 N- D
( x6 S8 [' {& g. V# I; K解决方案: , E9 a& p# s0 j% E4 j( e! \, M; u 你没有指定前十名的方法,所以我假设最高排名更高? 9 v7 [) w" Y: zWith TopItems As SELECT C.CountryID AS CountryID C.CountryName AS Country Count(FirstName) AS Origin ROW_NUMBER() OVER( ORDER BY Count(FirstName) DESC ) As Num FROM Users AS U JOIN Country AS C ON C.CountryID = U.CountryOfOrgin GROUP BY C.CountryName,C.CountryID )Select CountryId,Country,OriginFrom TopItemsWhere Num