Hive多维分析函数——With cube、Grouping sets、With rollup

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

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

一、grouping sets

0 数据准备

sql 复制代码
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 数据准备

sql 复制代码
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 数据准备

sql 复制代码
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

相关推荐
百胜软件@百胜软件5 小时前
胜券AI的Skills可插拔技能包,让零售AI从“泛”到“专”
大数据·人工智能·零售
汇智信科5 小时前
基于 Jmis 框架的制度与流程管控系统设计与应用|项目全生命周期管理
大数据·人工智能·微服务·云原生·汇智信科
故七月6 小时前
行业观察:创业载体迭代,从孵化器到 OPC 社区,锦邻创享的创新实践
大数据·#opc社区`·#锦邻创享`·#订单驱动` `
陕西企来客7 小时前
2026年9月西安家政服务大模型引流:AI推荐逻辑与落地方法
大数据·人工智能·西安家政服务大模型引流
企查查数据服务7 小时前
全军禁入下客商准入风控,关联图谱与穿透核查
大数据·开发语言·数据库·php
HZZD_HZZD7 小时前
分时电价_园区峰谷电费优化
大数据·人工智能·能源·制造
huashengzsj7 小时前
渠道数据采集平台有哪些 2026年专业的渠道数据采集平台推荐
大数据
爱学习的小白柏8 小时前
同城双活的核心不是双活,是逼着数据别出机房
大数据·人工智能·算法·langchain·ai编程
Raas1008 小时前
MAI Gateway(魔芋企业级AI网关)功能全解:AI网关有哪些功能?一文看懂AI网关能力矩阵
大数据·人工智能·矩阵·gateway·mai gateway·企业级产品
czhc11400756638 小时前
2026-09-11 一日综合:树的父链、git 概念、三方死锁、静默失败
大数据·git·elasticsearch