CASE WHEN语句提供了一种在 SQL 查询中根据不同条件执行逻辑的灵活方法。它可用于简单的条件检查,也可用于复杂的逻辑操作。使用CASE WHEN可以使查询更具可读性,并且可以减少在应用程序代码中进行逻辑操作的需要。
sql中的 case when用法详解
在 sql 中,
case when
语句通常用于根据条件执行不同的操作。
1. 基本用法
select
case
when condition1 then result1
when condition2 then result2
...
else default_result
end as alias_name
from
table_name;
case
开始一个 case when
语句块,end
结束该块。- 按顺序检查每个条件,并在找到第一个为真的条件时返回相应的结果。
- 如果没有条件为真,则返回
else
部分的默认结果(可选)。 - 允许在
when
子句中使用表达式作为条件,而不仅仅是列或单个值。
2. 举例
根据学生的成绩, 分为 a b c 和 d 四个等级, a: 大于等于90; b: [80,90); c: [60,80); d:小于60;
with t_score_info as (
select * from (
values (1001, 'lyf', 83),
(1002, 'zyb', 72),
(1003, 'whl', 45),
(1004, 'lxm', 64),
(1005, 'szy', 92),
(1006, 'xjp', 31),
(1007, 'ply', 42),
(1008, 'wyb', 88),
(1009, 'spx', 72),
(1010, 'yjj', 88),
(1011, 'teg', 98),
(1012, 'hxj', 61)
) as table_name(user_id, name, score)
)
select user_id, name, score
, case
when score >= 90 then 'a'
when score >= 80 and score < 90 then 'b'
when score >= 60 and score < 80 then 'c'
else 'd'
end as grades
from t_score_info
;
user_id | name | score | grades |
---|
1011 | teg | 98 | a |
1005 | szy | 92 | a |
1008 | wyb | 88 | b |
1010 | yjj | 88 | b |
1001 | lyf | 83 | b |
1002 | zyb | 72 | c |
1009 | spx | 72 | c |
1004 | lxm | 64 | c |
1012 | hxj | 61 | c |
1003 | whl | 45 | d |
1007 | ply | 42 | d |
1006 | xjp | 31 | d |
3. 使用 case when 语句进行数据分组
with t_score_info as (
select * from (
values (1001, 'lyf', 83, 26),
(1002, 'zyb', 72, 37),
(1003, 'whl', 45, 25),
(1004, 'lxm', 64, 46),
(1005, 'szy', 92, 36),
(1006, 'xjp', 31, 65),
(1007, 'ply', 42, 67),
(1008, 'wyb', 88, 25),
(1009, 'spx', 72, 46),
(1010, 'yjj', 88, 16),
(1011, 'teg', 98, 18),
(1012, 'hxj', 61, 48)
) as table_name(user_id, name, score, age)
)
select
case
when age >= 60 then '老年'
when age >= 40 and age < 60 then '中年'
when age >= 20 and age < 40 then '青年'
else '少年'
end as age_group
, count(1) as ct
from t_score_info
group by age_group
;
4. 使用 case when 语句修改字段值
with t_score_info as (
select * from (
values (1001, 'lyf', 83, 26, 'c'),
(1002, 'zyb', 72, 37, 'b'),
(1003, 'whl', 45, 25, 'c'),
(1004, 'lxm', 64, 46, 'c'),
(1005, 'szy', 92, 36, 'b'),
(1006, 'xjp', 31, 65, 'c'),
(1007, 'ply', 42, 67, 'b'),
(1008, 'wyb', 88, 25, 'c'),
(1009, 'spx', 72, 46, 'b'),
(1010, 'yjj', 88, 16, 'b'),
(1011, 'teg', 98, 18, 'a'),
(1012, 'hxj', 61, 48, 'b')
) as table_name(user_id, name, score, age, grades)
)
select user_id, name
, case
when score >= 90 then '90+'
when score >= 80 and score < 90 then '80-90'
when score >= 60 and score < 80 then '60-80'
else '0-60'
end as score
from t_score_info
;
user_id | name | score |
---|
1001 | lyf | 80-90 |
1002 | zyb | 60-80 |
1003 | whl | 0-60 |
1004 | lxm | 60-80 |
1005 | szy | 90+ |
1006 | xjp | 0-60 |
1007 | ply | 0-60 |
1008 | wyb | 80-90 |
1009 | spx | 60-80 |
1010 | yjj | 80-90 |
1011 | teg | 90+ |
1012 | hxj | 60-80 |
5. case when 和 聚合函数 一起使用
with t_score_info as (
select * from (
values (1001, 'lyf', 83, 26, 'c'),
(1002, 'zyb', 72, 37, 'b'),
(1003, 'whl', 45, 25, 'c'),
(1004, 'lxm', 64, 46, 'c'),
(1005, 'szy', 92, 36, 'b'),
(1006, 'xjp', 31, 65, 'c'),
(1007, 'ply', 42, 67, 'b'),
(1008, 'wyb', 88, 25, 'c'),
(1009, 'spx', 72, 46, 'b'),
(1010, 'yjj', 88, 16, 'b'),
(1011, 'teg', 98, 18, 'a'),
(1012, 'hxj', 61, 48, 'b')
) as table_name(user_id, name, score, age, grades)
)
select
sum(case when age >= 60 then score else 0 end) as `老年组总分`
, sum(case when age >= 40 and age < 60 then score else 0 end) as `中年组总分`
, sum(case when age >= 20 and age < 40 then score else 0 end) as `青年组总分`
, sum(case when age < 20 then score else 0 end) as `少年组总分`
from t_score_info
;
老年组总分 | 中年组总分 | 青年组总分 | 少年组总分 |
---|
73 | 197 | 380 | 186 |
6. case when 的嵌套使用
with t_score_info as (
select * from (
values (1001, 'lyf', 83, 26, 'c'),
(1002, 'zyb', 72, 37, 'b'),
(1003, 'whl', 45, 25, 'c'),
(1004, 'lxm', 64, 46, 'c'),
(1005, 'szy', 92, 36, 'b'),
(1006, 'xjp', 31, 65, 'c'),
(1007, 'ply', 42, 67, 'b'),
(1008, 'wyb', 88, 25, 'c'),
(1009, 'spx', 72, 46, 'b'),
(1010, 'yjj', 88, 16, 'b'),
(1011, 'teg', 98, 18, 'a'),
(1012, 'hxj', 61, 48, 'b')
) as table_name(user_id, name, score, age, grades)
)
select user_id, name, score, age,
case
when age >= 60 then
case
when score*1.5 >= 90 then 'a'
when score*1.5 >= 80 and score*1.5 < 90 then 'b'
when score*1.5 >= 60 and score*1.5 < 80 then 'c'
else 'd' end
when age >= 40 and age < 60 then
case
when score*1.2 >= 90 then 'a'
when score*1.2 >= 80 and score*1.2 < 90 then 'b'
when score*1.2 >= 60 and score*1.2 < 80 then 'c'
else 'd' end
when age >= 20 and age < 40 then
case
when score*1.0 >= 90 then 'a'
when score*1.0 >= 80 and score*1.0 < 90 then 'b'
when score*1.0 >= 60 and score*1.0 < 80 then 'c'
else 'd' end
else
case
when score*1.0 >= 90 then 'a'
when score*1.0 >= 80 and score*1.0 < 90 then 'b'
when score*1.0 >= 60 and score*1.0 < 80 then 'c'
else 'd' end
end as grades
from t_score_info
;
user_id | name | score | age | grades |
---|
1005 | szy | 92 | 36 | a |
1011 | teg | 98 | 18 | a |
1001 | lyf | 83 | 26 | b |
1009 | spx | 72 | 46 | b |
1008 | wyb | 88 | 25 | b |
1010 | yjj | 88 | 16 | b |
1012 | hxj | 61 | 48 | c |
1004 | lxm | 64 | 46 | c |
1007 | ply | 42 | 67 | c |
1002 | zyb | 72 | 37 | c |
1003 | whl | 45 | 25 | d |
1006 | xjp | 31 | 65 | d |
总结:
case when
语句提供了一种在 sql 查询中根据不同条件执行逻辑的灵活方法。它可用于简单的条件检查,也可用于复杂的逻辑操作。使用 case when
可以使查询更具可读性,并且可以减少在应用程序代码中进行逻辑操作的需要。
end
发表评论