如何在SQL Server中获得前50个国家/地区并将其余国家/地区分组给其他国家/地区

时间:2019-06-26 14:35:27

标签: sql sql-server

我希望看到前50个国家/地区,其余国家/地区需要使用CASE语句与其他国家/地区分组。那有可能吗?请提出建议。

1 个答案:

答案 0 :(得分:0)

您可以使用窗口函数和聚合来完成此操作

select (case when seqnum <= 50 then country else 'others' end) as country,
       sum(?)
from (select t.*, row_number() over (order by ?) desc as seqnum
      from t
     ) t
group by (case when seqnum <= 50 then country else 'others' end)
order by 2 desc;

?用于您关注的列。