----1 统计公司各省份人数 取前7个 其他----
select * from
( --统计前7个省份的人数
(select top 7 torigo, COUNT(*) tcount from person_info group by torigo order by tcount desc )
--合并查询结果 合并其他省份的人数
union
( --合并第8名之后的 为 其他省份 人数
select '其他省份' torigo ,SUM(t3.tcount) tcount from
(
--统计从第8名之后的省份人数
select *from
(
select ROW_NUMBER() OVER(Order by t1.tcount desc ) as rowin,* from
(
select torigo, COUNT(*) tcount from person_info group by torigo
) t1
) t2 where t2.rowin>7
) t3
)
--人数从大到小排序
) as t4 order by t4.tcount desc
sql server 统计各省份人数前几名和其他
最新推荐文章于 2023-01-03 21:18:29 发布