sql server在group by 中使用 case when

 


【1】需求

如何统计分类数量?

有些分类要合并统计, 例如: 统计亚洲和欧洲有多少国家,剩下的国家统计到"火星"

  要求结果是这样的:

    

 

【2】在sql group by 中使用 case when

【2.1】常规正向写法

该部分有误,请忽略

复制代码
;WITH t1 AS (
SELECT 'Asia' Area,'china' country UNION all
SELECT 'Asia' Area,'russia' country UNION all
SELECT 'europe' Area,'england' country UNION all
SELECT 'europe' Area,'germany' country UNION all
SELECT 'Africa' area,'india' country
)
SELECT CASE WHEN Area='china' THEN 'Asua'
            WHEN Area='russia' THEN ''
WHEN
ELSE 'spark' END AS AreaName,COUNT(1) country_num FROM t1 GROUP BY CASE WHEN Area='Asia' THEN Area WHEN Area='europe' THEN Area ELSE 'spark' end
复制代码

  

 

 

 这个写法固然可以,但如果不只是亚洲、欧洲,而是有十几个甚至几十个相关项,难道要一个一个 when  Area=value1 when Area=value2......Area=valueN 吗? 

显示是不合适且冗余复杂的,这个时候就用到我们下面的【2.2】写法

【2.2】反向批量写法

把亚洲欧洲归到一类,其他的归到另一类

复制代码
;WITH t1 AS (
SELECT 'Asia' Area,'china' country UNION all
SELECT 'Asia' Area,'russia' country UNION all
SELECT 'europe' Area,'england' country UNION all
SELECT 'europe' Area,'germany' country UNION all
SELECT 'Africa' area,'india' country
)
SELECT CASE WHEN Area IN ('Asia','europe') THEN 'Area_other'
            ELSE 'spark' END AS AreaName,COUNT(1) country_num
            
FROM t1 
GROUP BY CASE WHEN Area IN ('Asia','europe') THEN 'Area_other'
            ELSE 'spark' end
复制代码

  

 

 

 

 

 或者也可以反过来用 not in

复制代码
;WITH t1 AS (
SELECT 'Asia' Area,'china' country UNION all
SELECT 'Asia' Area,'russia' country UNION all
SELECT 'europe' Area,'england' country UNION all
SELECT 'europe' Area,'germany' country UNION all
SELECT 'Africa' area,'india' country
)
SELECT CASE WHEN Area NOT IN ('Asia','europe') THEN 'spark'
            ELSE Area END AS AreaName,COUNT(1) country_num
            
FROM t1 
GROUP BY CASE WHEN Area NOT IN ('Asia','europe') THEN 'spark'
            ELSE Area end
复制代码

  

【3】关于分数计数的案例

需求:

  

 

 解决:

复制代码
WITH t1 AS (
SELECT 'a' as name,100 score UNION all
SELECT 'b' ,99 score UNION all
SELECT 'c' ,80 score UNION all
SELECT 'd' ,59 score UNION all
SELECT 'e' ,40 score
)
select case when score >=90 then '优秀' when score >=80 then '良好' when score >=60 then '及格'  else '不及格' end
,count(1) as num 
FROM t1
group by score

----------------------------------------------------------

;WITH t1 AS (
SELECT 'a' as name,100 score UNION all
SELECT 'b' ,99 score UNION all
SELECT 'c' ,80 score UNION all
SELECT 'd' ,59 score UNION all
SELECT 'e' ,40 score
)
select case when score >=90 then '优秀' when score >=80 then '良好' when score >=60 then '及格'  else '不及格' end
,count(1) as num 
FROM t1
group by case when score >=90 then '优秀' when score >=80 then '良好' when score >=60 then '及格'  else '不及格' end
复制代码

  

 

 第二段代码正确,实现了我们的需求

posted @   郭大侠1  阅读(5765)  评论(2编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2019-08-07 mysql大数据量插入参考
2018-08-07 (4.6)数据页深入探索--内部探索聚集索引
2018-08-07 (4.11)DBCC 常用命令
2018-08-07 sql server监控图解
2018-08-07 (4.4)dbcc checkdb 数据页修复
点击右上角即可分享
微信分享提示