Sql case when 用法实例详解

sql中case when还是比较常见的用法。经常会出现一段时间不用,相关写法与语法又记不太清楚的情况。干脆就记一下,以备不时之需。

1.准备数据

现在有一张表xxx,里面有一个字段叫basicinfo_credit_status,类型为整数,1,2,3,4,5这种。其中1表示授信通过,4表示授信拒绝。

2.case的两种写法

case一般有两种写法: 简单case函数和case搜索函数。

简单case函数的写法如下

case basicinfo_credit_status
	when 1 then "pass"
	when 4 then "reject"
	else "other"
end

case搜索函数的写法如下

case 
when basicinfo_credit_status = 1 then "pass"
when basicinfo_credit_status = 4 then "reject"
else "other"
end

对照上面两种写法,不难看出其优缺点:
简单case函数的写法比较简洁明了,能实现基本的功能与需求。
搜索函数代码相对较为复杂,但是也能实现一些比较复杂的功能,比如复杂的判定逻辑。

select basicinfo_credit_status,
 (case basicinfo_credit_status when 1 then "pass" when 4 then "reject" else "other" end) as status
  from xxx limit 20;
+--------------------------+---------+--+
| basicinfo_credit_status  | status  |
+--------------------------+---------+--+
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 2                        | other   |
| 1                        | pass    |
| 1                        | pass    |
| 3                        | other   |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 2                        | other   |
| 3                        | other   |
| 1                        | pass    |
| 1                        | pass    |
+--------------------------+---------+--+
select basicinfo_credit_status, 
(case when basicinfo_credit_status = 1 then "pass" when basicinfo_credit_status = 4 then "reject" else "other" end) as status 
from xxx limit 20;
+--------------------------+---------+--+
| basicinfo_credit_status  | status  |
+--------------------------+---------+--+
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 2                        | other   |
| 1                        | pass    |
| 1                        | pass    |
| 3                        | other   |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 1                        | pass    |
| 2                        | other   |
| 3                        | other   |
| 1                        | pass    |
| 1                        | pass    |
+--------------------------+---------+--+

3.分段统计

case语句经常与sum语句一起使用,可以实现分段统计的功能。
比如我们需要统计授信通过的人数是多少,授信未通过的人数是多少,可以用如下sql语句。

select 
sum(case when basicinfo_credit_status = 1 then 1 else 0 end) as is_credit_pass,
sum(case when basicinfo_credit_status = 4 then 1 else 0 end) as is_credit_reject 
from xxx;

最后可以得到结果

+-----------------+-------------------+--+
| is_credit_pass  | is_credit_reject  |
+-----------------+-------------------+--+
| num1         | num2          |
+-----------------+-------------------+--+

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值