有些指标涉及【多维度】的聚合,大的汇总维度,小的明细维度,需要精细化的下钻。
with test1 as (select '2021-08' as month_date,'2021-08-11' as day_date,10 as pv union all select '2021-08' as month_date,'2021-08-10' as day_date,15 as pv union all select '2021-08' as month_date,'2021-08-08' as day_date,35 as pv union all select '2021-07' as month_date,'2021-07-08' as day_date,35 as pv union all select '2021-07' as month_date,'2021-07-06' as day_date,25 as pv union all select '2021-07' as month_date,'2021-07-03' as day_date,15 as pv) select month_date, day_date, sum(pv) as pv from test1 group by month_date,day_date grouping sets ( (), (month_date), (month_date,day_date), (day_date) ) order by month_date,day_date;
with test1 as (select '2021-08' as month_date,'2021-08-11' as day_date,10 as pv union all select '2021-08' as month_date,'2021-08-10' as day_date,15 as pv union all select '2021-08' as month_date,'2021-08-08' as day_date,35 as pv union all select '2021-07' as month_date,'2021-07-08' as day_date,35 as pv union all select '2021-07' as month_date,'2021-07-06' as day_date,25 as pv union all select '2021-07' as month_date,'2021-07-03' as day_date,15 as pv) select month_date, day_date, sum(pv) as pv from test1 group by month_date,day_date with cube order by month_date,day_date;
with test1 as (select '2021-08' as month_date,'2021-08-11' as day_date,10 as pv union all select '2021-08' as month_date,'2021-08-10' as day_date,15 as pv union all select '2021-08' as month_date,'2021-08-08' as day_date,35 as pv union all select '2021-07' as month_date,'2021-07-08' as day_date,35 as pv union all select '2021-07' as month_date,'2021-07-06' as day_date,25 as pv union all select '2021-07' as month_date,'2021-07-03' as day_date,15 as pv) select month_date, day_date, sum(pv) as pv from test1 group by month_date,day_date with rollup order by month_date,day_date;
从结果上可以看出,with rollup 和with cube的区别是,少了day_date这个单独维度的聚合,因为with rollup是以左侧维度为主,当左侧month_date维度为NULL时,右侧day_date维度必须为NULL。
参考文章 :
https://zhuanlan.zhihu.com/p/631268351
https://blog.51cto.com/u_14555/6696007