Hive高级聚合GROUPING SETS,ROLLUP以及CUBE
scala> import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.sql.hive.HiveContext
scala> val hcon=new HiveContext(sc)
warning: there was one deprecation warning; re-run with -deprecation for details
hcon: org.apache.spark.sql.hive.HiveContext = org.apache.spark.sql.hive.HiveContext@dd102ea
scala> hcon.sql("select age,sex,count(1) from gamedw.customers group by age,sex").show
+---+---+--------+
|age|sex|count(1)|
+---+---+--------+
| 56| 0| 7|
| 32| 1| 7|
| 20| 1| 7|
| 50| 1| 7|
| 5| 1| 4|
| 47| 0| 7|
| 85| 1| 7|
|100| 0| 5|
+---+---+--------+
scala> hcon.sql("select age,sex,count(1) from gamedw.customers group by age,sex grouping sets((age,sex),sex,())").show
+----+----+--------+
| age| sex|count(1)|
+----+----+--------+
| 56| 0| 7|
|null| 1| 32|
| 20| 1| 7|
|null|null| 51|
| 32| 1| 7|
| 5| 1| 4|
| 85| 1| 7|
| 47| 0| 7|
| 100| 0| 5|
|null| 0| 19|
| 50| 1| 7|
+----+----+--------+
GROUPING SETS
在一个GROUP BY查询中,根据不同的维度组合进行聚合,等价于将不同维度的GROUP BY结果集进行UNION ALL,SETS的子句中如果包含()数据集,则表示整体聚合
scala> hcon.sql("select age,sex,count(1) from gamedw.customers group by age,sex grouping sets((age,sex),sex,()) order by age,sex").show
+----+----+--------+
| age| sex|count(1)|
+----+----+--------+
|null|null| 51|
|null| 0| 19|
|null| 1| 32|
| 5| 1| 4|
| 20| 1| 7|
| 32| 1| 7|
| 47| 0| 7|
| 50| 1| 7|
| 56| 0| 7|
| 85| 1| 7|
| 100| 0| 5|
+----+----+--------+
scala> hcon.sql("select age,sex,count(1) from gamedw.customers group by age,sex grouping sets((age,sex),sex,age,()) order by age,sex").show
+----+----+--------+
| age| sex|count(1)|
+----+----+--------+
|null|null| 51|
|null| 0| 19|
|null| 1| 32|
| 5|null| 4|
| 5| 1| 4|
| 20|null| 7|
| 20| 1| 7|
| 32|null| 7|
| 32| 1| 7|
| 47|null| 7|
| 47| 0| 7|
| 50|null| 7|
| 50| 1| 7|
| 56|null| 7|
| 56| 0| 7|
| 85|null| 7|
| 85| 1| 7|
| 100|null| 5|
| 100| 0| 5|
+----+----+--------+
CUBE
根据GROUP BY的维度的所有组合进行聚合。
scala> hcon.sql("select age,sex,count(1) from gamedw.customers group by age,sex with cube order by age,sex").show
+----+----+--------+
| age| sex|count(1)|
+----+----+--------+
|null|null| 51|
|null| 0| 19|
|null| 1| 32|
| 5|null| 4|
| 5| 1| 4|
| 20|null| 7|
| 20| 1| 7|
| 32|null| 7|
| 32| 1| 7|
| 47|null| 7|
| 47| 0| 7|
| 50|null| 7|
| 50| 1| 7|
| 56|null| 7|
| 56| 0| 7|
| 85|null| 7|
| 85| 1| 7|
| 100|null| 5|
| 100| 0| 5|
+----+----+--------+
ROLLUP
是CUBE的子集,以最左侧的维度为主,从该维度进行层级聚合。
scala> hcon.sql("select age,sex,count(1) from gamedw.customers group by age,sex with rollup order by age,sex").show
+----+----+--------+
| age| sex|count(1)|
+----+----+--------+
|null|null| 51|
| 5|null| 4|
| 5| 1| 4|
| 20|null| 7|
| 20| 1| 7|
| 32|null| 7|
| 32| 1| 7|
| 47|null| 7|
| 47| 0| 7|
| 50|null| 7|
| 50| 1| 7|
| 56|null| 7|
| 56| 0| 7|
| 85|null| 7|
| 85| 1| 7|
| 100|null| 5|
| 100| 0| 5|
+----+----+--------+
Hive高级聚合GROUPING SETS,ROLLUP以及CUBE的更多相关文章
- SQL GROUP BY GROUPING SETS,ROLLUP,CUBE(需求举例)
实现按照不同级别分组统计 关于GROUP BY 中的GROUPING SETS,ROLLUP,CUBE 从需求的角度理解会更加容易些. 需求举例: 假如一所学校只有两个系, 每个系有两个专业, 每个专 ...
- Hive函数:GROUPING SETS,GROUPING__ID,CUBE,ROLLUP
参考:lxw大数据田地:http://lxw1234.com/archives/2015/04/193.htm 数据准备: CREATE EXTERNAL TABLE test_data ( mont ...
- (2.4)DDL增强功能-数据汇总grouping、rollup、cube
参考:https://www.cnblogs.com/nikyxxx/archive/2012/11/27/2791001.html 1.rollup (1)rollup在group by 子句中使用 ...
- 9.hive聚合函数,高级聚合,采样数据
本文主要使用实例对Hive内建的一些聚合函数.分析函数以及采样函数进行比较详细的讲解. 一.基本聚合函数 数据聚合是按照特定条件将数据整合并表达出来,以总结出更多的组信息.Hive包含内建的一些基本聚 ...
- TSQL 分组集(Grouping Sets)
分组集(Grouping Sets)是多个分组的并集,用于在一个查询中,按照不同的分组列对集合进行聚合运算,等价于对单个分组使用“union all”,计算多个结果集的并集.使用分组集的聚合查询,返回 ...
- Hive高阶聚合函数 GROUPING SETS、Cube、Rollup
-- GROUPING SETS作为GROUP BY的子句,允许开发人员在GROUP BY语句后面指定多个统计选项,可以简单理解为多条group by语句通过union all把查询结果聚合起来结合起 ...
- 高级聚合函数rollup(),cube(),grouping sets()
rollup(),cube(),grouping sets() 上面这几个函数,是对group by分组功能做的功能扩展. a.rollup() 功能:在原结果基础上追加一行总合计记录 ...
- [转]详解Oracle高级分组函数(ROLLUP, CUBE, GROUPING SETS)
原文地址:http://blog.csdn.net/u014558001/article/details/42387929 本文主要讲解 ROLLUP, CUBE, GROUPING SETS的主要用 ...
- Hive学习之路 (十七)Hive分析窗口函数(五) GROUPING SETS、GROUPING__ID、CUBE和ROLLUP
概述 GROUPING SETS,GROUPING__ID,CUBE,ROLLUP 这几个分析函数通常用于OLAP中,不能累加,而且需要根据不同维度上钻和下钻的指标统计,比如,分小时.天.月的UV数. ...
随机推荐
- TableLayoutPanel 行高列宽设置
/// <summary> /// 获取TableLayoutPanel指定行的高度 /// </summary> /// <param name="layou ...
- mongodb 安装(官方说明链接)
这里面有很全的 https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/ 先截个图吧,都是标准的流程,按照操作,是可以安 ...
- 黄聪:php精度计算问题
如果用php的+-*/计算浮点数的时候,可能会遇到一些计算结果错误的问题,比如echo intval( 0.58*100 );会打印57,而不是58,这个其实是计算机底层二进制无法精确表示浮点数的一个 ...
- PHP 下载中文乱码解决
利用 iconv() 函数解决乱码 $file_name = iconv("utf-8","gb2312",$file_name); 原文链接 http://m ...
- slot内容分发
vue实现了一套内容分发的API,这套API基于当前的web components规范草案,将<slot>元素作为承载分发内容的出口. 在前面的父子组件中,我们提到过,在vue中,组件实例 ...
- 补充appium -api
//锁屏 driver.lockScreen(2); //判断是否锁屏 driver.isLocked(); //截屏并保存至本地 File screen = driver.getScreenshot ...
- appium的log详细分析
下面介绍appium日志的大概分析 //启动appium服务成功2017-03-24 11:22:49:218 - info: [Appium] Welcome to Appium v1.6.3201 ...
- Ajax的总结
1.运行Ajax的环境,在服务器上才可以实现他的功能,客户端等别的地方,虽然也可以运行,但是功能一定是不全的,有可能很多东西都不会发生反应: 2.传参 (只写关键步骤) (必须在服务器上运行) ge ...
- rhel7.0解决:This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
看这篇文章前,先说一下我的实际情况.本来要部署docker服务的,然后yum安装任何软件都不起效果,最后通过老师远程的帮助,最后成功安装上docker,老师的解决办法就是忽略这个问题,直接自己配置网络 ...
- [UE4]使用DataTable