数据分析中常常需要各种不同维度的汇总,下面介绍如何让sql像excel透视表一样汇总,可以节省不少行代码
使用条件:必须与group by一起使用,放在group by 之后
优化:汇总行会出现null值,需要用到grouping一步修改null值
三种汇总区别:
grouping sets : 指定任意组合进行分组;
with rollup: 递进式层级进行分组,因此group by 后的字段顺序不同会有不同的分组结果;
with cube: 对每个可能的组合分组。
GROUPING SETS
select channel
,isconfirm
,star
,count(distinct orderid) sales
from talble
group by channel
,isconfirm
,star
grouping sets(channel,isconfirm,(star,isconfirm))
等价于:
select channel,NULL,NULL
,count(distinct orderid) sales
from table
group by channel
union all
select NULL,isconfirm,NULL
,count(distinct orderid) sales
from table
group by isconfirm
union all
select NULL,isconfirm,star
,count(distinct orderid) sales
from table
group by isconfirm,star
WIHT ROLLUP
select channel
,isconfirm
,star
,count(distinct orderid) sales
from talble
group by channel
,isconfirm
,star
with rollup
下钻过程:总体→channel→channel+isconfrim
调换group by之后的字段顺序,分组也发生了改变
select channel
,isconfirm
,star
,count(distinct orderid) sales
from talble
group by star
,isconfirm
,channel
with rollup
下钻过程:总体→star→star+isconfirm
WITH CUBE
所有可能的分组
select channel
,isconfirm
,star
,count(distinct orderid) sales
from talble
group by channel
,isconfirm
,star
with cube
用grouping 修改NULL值
select if(grouping(channel)=1,'整体',channel) channel
,if(grouping(isconfirm)=1,'整体',isconfirm) isconfirm
,if(star)=1,'整体',star) star
,count(distinct orderid) sales
from talble
group by channel
,isconfirm
,star
with cube