SQL中CASE WHEN THEN END作用及用法详解

本文探讨了SQL中case函数的使用,包括简单case和case搜索函数,展示了如何在统计分析、数据分组和条件判断中灵活应用。通过实例解析了如何统计不同区域人口、工资等级和多元条件分组,强调了case函数在数据处理中的强大之处,以及在满足公司规则检查中的优势。
摘要由CSDN通过智能技术生成

sql case when then else end
查询生成统计列表:

SELECT a.managecom,
       a.subtype,
       count(*) loadsucc,
       sum(case when a.state in  ('4', '5', '6', '7', '8', '9') then 1 else 0 end) recogsucc,
       sum(case when a.state in  ('3', '12', '13') then 1 else 0 end) recogfail,
       sum(case when a.state in  ('1', '2') then 1 else 0 end) waitrecog
  FROM ocr_docdetail a, ocr_loaddetail c
 WHERE 1 = 1
   and a.managecom like '86%'
   and a.managecom = c.managecom
   and a.bussno = c.bussno
   and a.subtype = c.subtype
   and c.loadstate = 0
   and c.scandate >= date '2012-07-29'
   and c.scandate <= date '2013-01-07'
 group by a.managecom, a.subtype
 order by a.managecom, a.subtype;

case具有两种格式。简单case函数和case搜索函数。
–简单case函数

case sex

            when '1' then '男'

            when '2' then '女'

            else '其他' end
--case搜索函数

            case when sex = '1' then '男'

            when sex = '2' then '女'

            else '其他' end

这两种方式,可以实现相同的功能。简单case函数的写法相对比较简洁,但是和case搜索函数相比,功能方面会有些限制,比如写判定式。

下面我们来看一下,使用case函数都能做些什么事情。

一,已知数据按照另外一种方式进行分组,分析。

有如下数据:(为了看得更清楚,我并没有使用国家代码,而是直接用国家名作为primary key)

国家(country)人口(population)
中国600
美国100
加拿大100
英国200
法国300
日本250
德国200
印度250

根据这个国家人口数据,统计亚洲和北美洲的人口数量。应该得到下面这个结果。

人口
亚洲1100
北美洲250
其他700

想要解决这个问题,你会怎么做?生成一个带有洲code的view,是一个解决方法,但是这样很难动态的改变统计的方式。
假如使用case函数,sql代码如下:

select sum(population),

            case country

            when '中国'     then'亚洲'

            when '印度'     then'亚洲'

            when '日本'     then'亚洲'

            when '美国'     then'北美洲'

            when '加拿大'  then'北美洲'

            when '墨西哥'  then'北美洲'

            else '其他' end

            from   table_a

            group by case country

            when '中国'     then'亚洲'

            when '印度'     then'亚洲'

            when '日本'     then'亚洲'

            when '美国'     then'北美洲'

            when '加拿大'  then'北美洲'

            when '墨西哥'  then'北美洲'

            else '其他' end;

同样的,我们也可以用这个方法来判定工资的等级,并统计每一等级的人数。sql代码如下;

select

            case when salary <= 500 then '1'

            when salary > 500 and salary <= 600  then '2'

            when salary > 600 and salary <= 800  then '3'

            when salary > 800 and salary <= 1000 then '4'

            else null end salary_class,

            count(*)

            from   table_a

            group by

            case when salary <= 500 then '1'

            when salary > 500 and salary <= 600  then '2'

            when salary > 600 and salary <= 800  then '3'

            when salary > 800 and salary <= 1000 then '4'

            else null end;

二,用一个sql语句完成不同条件的分组。

国家(country)性别(sex)人口(population)
中国1340
中国2260
美国145
美国255
加拿大151
加拿大249
英国140
英国260

按照国家和性别进行分组,得出结果如下

国家(country)
中国340240
美国4555
加拿大15149
英国4060

普通情况下,用union也可以实现用一条语句进行查询。但是那样增加消耗(两个select部分),而且sql语句会比较长。
下面是一个是用case函数来完成这个功能的例子

select country,

            sum( case when sex = '1' then

            population else 0 end),  --男性人口

            sum( case when sex = '2' then

            population else 0 end)   --女性人口

            from table_a

            group by country;

这样我们使用select,完成对二维表的输出形式,充分显示了case函数的强大。

这样我们使用select,完成对二维表的输出形式,充分显示了case函数的强大。

在check中使用case函数在很多情况下都是非常不错的解决方法。可能有很多人根本就不用check,那么我建议你在看过下面的例子之后也尝试一下在sql中使用check。
下面我们来举个例子
公司a,这个公司有个规定,女职员的工资必须高于1000块。假如用check和case来表现的话,如下所示

constraint check_salary check

            ( case when sex = '2'

            then case when salary > 1000

            then 1 else 0 end

            else 1 end = 1 )

假如单纯使用check,如下所示

constraint check_salary check

            ( sex = '2' and salary > 1000 )

女职员的条件倒是符合了,男职员就无法输入了。

原文链接:https://blog.csdn.net/xuxurui007/article/details/8479953

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值