Sql case when 用法实例详解

Sql case when 用法实例详解sql中casewhen还是比较常见的用法。经常会出现一段时间不用,相关写法与语法又记不太清楚的情况。干脆就记一下,以备不时之需。1.准备数据现在有一张表xxx,里面有一个字段叫basicinfo_credit_status,类型为整数,1,2,3,4,5这种。其中1表示授信通过,4表示授信拒绝。2.case的两种写法case一般有两种写法:简单case函数和case搜索函数。简单case函数的写法如下casebasicinfo_credit_status when1then”pa

大家好,欢迎来到IT知识分享网。

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          |
+-----------------+-------------------+--+

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23996.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信