SQL技巧高级系列②——聚合函数和CASE WHEN的使用

本文介绍了SQL中的CASE WHEN语句,作为一种灵活的分类方法,它能处理连续型变量的分类汇总。CASE WHEN分为简单表达式和搜索表达式,适用于连续型、分类和字符串变量的处理,甚至可以实现行列转换。此外,文章还讲解了CASE WHEN与聚合函数如SUM和COUNT结合使用,用于在分类基础上进行计数和求和操作,以满足复杂的数据分析需求。
公众号: 数据小斑马,关注即可获得价值1000元的数据分析学习资料

SQL系列目录(文末有大奖赠送):
SQL技巧初级系列①—建表/更新表/删除表
SQL技巧初级系列②—聚合和排序(group by,having,order by)
SQL技巧初级系列③——数据拼接(集合运算union和列连接join)
SQL技巧中级系列①——字符串函数的使用
SQL技巧中级系列②——日期函数的使用
SQL技巧高级系列①——窗口分析函数的使用
Hive Sql 性能优化——看这一篇就够啦!

之前博客有提到过group by 具有分组统计功能,但是如果分组的字段是连续型变量,想要转化成分类变量再汇总,改如何处理呢?——Case when 就是最佳的解决办法

一、什么是CASE WHEN?
case when可以说是一种特定的分类方式,比Group by更加灵活,更能符合业务错综复杂的分类需求

二、CASE WHEN的用法
有简单case表达式和搜索case表达式两种,比如根据用户的充值金额区分为超大中小R等,简单case表达式如下:

Select dt,user,amount,
           case amount
           when >50000 then "超R”
           when >10000 then "大R“
           when >1000 then "中R"
           when >0 then "小R"
           else then "免费用户" 
           end 
           from user_recharging_record 

搜索表达式如下:

   Select dt,user,amount,
               case 
               when amount>50000 then "超R”
               when amount>10000 then "大R“
               when amount>1000 then "中R"
               when amount>0 then "小R"
               else then "免费用户" 
               end 
               from user_recharging_record 

看出不同点来了吗?简单表达式只能写一遍分类变量名称即可,更为简洁,但是搜索case表达式可以不限一个变量,如下

Select dt,user,amount,
               case 
               when amount>50000 and log_days>25 then "活跃超R”
               when amount>50000 and log_days<=25 then "一般超R“
               when amount>10000 and log_days>25  then "活跃大R"
               when amount>10000 and log_days<=25  then "一般大R"
               when amount>1000 and log_days>25  then "活跃中R"
               when amount>1000 and log_days<=25  then "一般中R"
               when amount>0 and log_days>25 then "活跃小R"
               when amount>0 and log_days<=25 then "活跃小R"
               else then "免费用户" 
               end 
               from user_recharging_record a join user_login_record b 
               on a.userid=b.userid

② 不仅是连续型变量,分类变量和字符串也可以用case when:

Select a.*, case
              when city in ("beijing","shanghai","shenzhen","guangzhou") then "一线城市"
              else "非一线城市"
              end  from user_infomatin a 

③ 可以通过case when进行行列转换

select userid,
           case when amount>10000 then "超R” else null end as “超R”
           case when amount>10000 then "大R“ else unll end as ”大R"
           case when amount>1000 then "中R" else null end as ”中R“
           case when amount>0 then ”小R“ else null then as ”小R"
           from user_recharging_record

三、CASE WHEN和聚合函数联合使用

公众号: 数据小斑马,关注即可获得价值1000元的数据分析学习资料

分类计数和求和,比如根据用户充值金额区分用户类型后,想计算不同类型的总充值金额、充值次数和充值人数

   Select dt,
               sum(case when amount>50000 then amount else 0 end as "超R充值金额"
               count(case when amount>50000 then 1 else 0 end as "超R充值次数"
               count(distinct case when amount>50000 then 1 else null end as "超R充值人数"
               sum(distinct case when amount>50000 then 1 else 0 end as "超R充值人数"
               from user_recharging_record 

本人互联网数据分析师,目前已出ExcelSQLPandasMatplotlibSeaborn机器学习统计学个性推荐关联算法工作总结系列。


微信搜索并关注 " 数据小斑马" 公众号,回复“sql”就可以免费领取下方sql必知必会、sql基础教程等9本sql入门到精通9本书籍

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值