Hive多维分析函数——With cube、Grouping sets、With rollup
创始人
2024-12-01 07:04:50
0

有些指标涉及【多维度】的聚合,大的汇总维度,小的明细维度,需要精细化的下钻。

  • grouping sets: 多维度组合,组合维度自定义;
  • with cube: 多维度组合,程序自由组合,组合为各个维度的笛卡尔积;
  • with rollup:是 with cube的子集,以左侧维度为主,即不允许左侧为NULL,右侧为非NULL的情况出现

一、grouping sets

 0 数据准备

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; 

 1 结果分析

二、with cube

 0 数据准备

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;

 1 结果分析

2 总结

  • 从with cube和grouping sets的案例可以看出,两个结果是一样的;
  • with cube的维度组合(各个维度的笛卡尔积)就是groupingsets里面手动添加的维度,即为month_date,day_date两个维度的笛卡尔积。维度组合即为:()、 (month_date)、 (month_date,day_date)、 (day_date)

三、 with rollup

 0 数据准备

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;

 1 结果分析

 2 总结

   从结果上可以看出,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

相关内容

热门资讯

终于找到“微信炸金花链接怎样弄... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
微信上金花房卡怎么来的/金花房... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
一分钟推荐“在哪里买炸金花房卡... 长虹大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
终于找到“购买斗牛房卡联系方式... 悠悠互娱是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来...
ia实测“购买斗牛房卡联系方式... 狂飙大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来...
正版授权“牛牛金花房卡是如何购... 新星游是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来...
金花房卡在哪获取/微信链接金花... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享受...
一分钟了解“购买斗牛房卡联系方... 随意玩俱乐部是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买...
一分钟推荐“微信斗牛房间怎么弄... 微信斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
一分钟了解“微信牛牛链接怎么制... 超圣大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:86909166许多玩家在游戏中会购买房卡来...
牛牛金花房卡链接在哪买/微信斗... 微信斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来...
微信斗牛牛房卡使用教程/微信炸... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
玩金花牛牛15元100张房卡代... 牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡来享...
金花链接的房卡在哪里买的/在哪... 金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
终于找到“微信金花房卡链接使用... 新众乐牛牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡...
ia实测“微信牛牛房卡购买方法... 新九天是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡来享...
微信炸金花房卡到哪里买/微信链... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
终于找到“金花大厅房卡链接在哪... 超圣大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来...
安卓系统机备用机,智能生活双倍... 你有没有想过,当你的安卓手机突然罢工,或者你急需一台备用机来处理一些紧急事务时,你会怎么办呢?别急,...
微信链接斗牛房卡开科技/微信金... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...