一篇文章带你了解SQL之CASE WHEN用法详解

目录
  • 简单case when函数:
  •  等同于,使用case when条件表达式函数实现:
  •  then后边的值与else后边的值类型应一致,否则会报错。如下:

简单case when函数:

case score when 'a' then '优' else '不及格' end
case score when 'b' then '良' else '不及格' end
case score when 'c' then '中' else '不及格' end

 等同于,使用case when条件表达式函数实现:

case when score = 'a' then '优'
     when score = 'b' then '良'
     when score = 'c' then '中' else '不及格' end

 then后边的值与else后边的值类型应一致,否则会报错。如下:

case score when 'a' then '优' else 0 end

‘优’和0数据类型不一致则报错: 

[err] ora-00932: 数据类型不一致: 应为 char, 但却获得 number

简单case when函数只能应对一些简单的业务场景,而case when条件表达式的写法则更加灵活。

case when条件表达式函数:类似java中的if else语句。

格式:

case when condition then result

[when...then...]

else result

end

condition是一个返回布尔类型的表达式,如果表达式返回true,则整个函数返回相应result的值,如果表达式皆为false,则返回else后result的值,如果省略了else子句,则返回null。

下面介绍几种常用场景。

场景1:有分数score,score<60返回不及格,score>=60返回及格,score>=80返回优秀

select
    student_name,
    (case when score < 60 then '不及格'
        when score >= 60 and score < 80 then '及格'
        when score >= 80 then '优秀'
        else '异常' end) as remark
from
    table

 注意:如果你想判断score是否null的情况,when score = null then ‘缺席考试’,这是一种错误的写法,正确的写法应为:

case when score is null then '缺席考试' else '正常' end

场景2:现老师要统计班中,有多少男同学,多少女同学,并统计男同学中有几人及格,女同学中有几人及格,要求用一个sql输出结果。

表结构如下:其中stu_sex字段,0表示男生,1表示女生。

stu_code stu_name stu_sex stu_score
xm 小明 0 88
xl 小磊 0 55
xf 小峰 0 45
xh 小红 1 66
xn 晓妮 1 77
xy 小伊 1 99
select 
	sum (case when stu_sex = 0 then 1 else 0 end) as male_count,
	sum (case when stu_sex = 1 then 1 else 0 end) as female_count,
	sum (case when stu_score >= 60 and stu_sex = 0 then 1 else 0 end) as male_pass,
	sum (case when stu_score >= 60 and stu_sex = 1 then 1 else 0 end) as female_pass
from 
	thtf_students

输出结果如下:

male_count female_count male_pass female_pass
3 3 1 3

场景3:经典行转列,并配合聚合函数做统计

现要求统计各个城市,总共使用了多少水耗、电耗、热耗,使用一条sql语句输出结果

有能耗表如下:其中,e_type表示能耗类型,0表示水耗,1表示电耗,2表示热耗

e_code e_value e_type
北京 28.50 0
北京 23.51 1
北京 28.12 2
北京 12.30 0
北京 15.46 1
上海 18.88 0
上海 16.66 1
上海 19.99 0
上海 10.05 0
select 
	e_code,
	sum(case when e_type = 0 then e_value else 0 end) as water_energy,--水耗
	sum(case when e_type = 1 then e_value else 0 end) as ele_energy,--电耗
	sum(case when e_type = 2 then e_value else 0 end) as heat_energy--热耗
from 
	thtf_energy_test
group by
	e_code

 输出结果如下

e_code water_energy ele_energy heat_energy
北京 40.80 38.97 28.12
上海 48.92 16.66 0

场景4:case when中使用子查询

根据城市用电量多少,计算用电成本。假设电能耗单价分为三档,根据不同的能耗值,使用相应价格计算成本。

 价格表如下:

p_price p_level p_limit
1.20 0 10
1.70 1 30
2.50 2 50

当能耗值小于10时,使用p_level=0时的p_price的值,能耗值大于10小于30使用p_level=1时的p_price的值…

case when energy <= (select p_limit from table_price where p_level = 0) then (select p_price from table_price where p_level = 0)
    when energy > (select p_limit from table_price where p_level = 0) and energy <= (select p_limit from table_price where p_level = 1) then (select p_price from table_price where p_level = 1)
    when energy > (select p_limit from table_price where p_level = 1) and energy <= (select p_limit from table_price where p_level = 2) then (select p_price from table_price where p_level = 2)

 场景5:结合max聚合函数

case when 函数使用起来简单易懂,此篇文章只作了简单的使用介绍,还需在实际工作中根据业务场景不同来灵活使用。

总结

本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐