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

你可能感兴趣的:(hive,sql,case,when)