Hive高阶聚合函数 GROUPING SETS、Cube、Rollup
-- GROUPING SETS作为GROUP BY的子句,允许开发人员在GROUP BY语句后面指定多个统计选项,可以简单理解为多条group by语句通过union all把查询结果聚合起来结合起来。
select
device_id
,os_id
,app_id
,count(user_id)
from test_xinyan_reg
group by device_id,os_id,app_id
grouping sets((device_id),(os_id),(device_id,os_id),())
-- 等价于
SELECT device_id,null,null,count(user_id) FROM test_xinyan_reg group by device_id UNION ALL
SELECT null,os_id,null,count(user_id) FROM test_xinyan_reg group by os_id UNION ALL
SELECT device_id,os_id,null,count(user_id) FROM test_xinyan_reg group by device_id,os_id UNION ALL
SELECT null,null,null,count(user_id) FROM test_xinyan_reg
; -- cube简称数据魔方,可以实现hive多个任意维度的查询,cube(a,b,c)则首先会对(a,b,c)进行group by,然后依次是(a,b),(a,c),(a),(b,c),(b),©,最后在对全表进行group by,他会统计所选列中值的所有组合的聚合
-- cube即为grouping sets的简化过程函数
select device_id,os_id,app_id,client_version,from_id,count(user_id)
from test_xinyan_reg
group by device_id,os_id,app_id,client_version,from_id with cube; -- rollup可以实现从右到做递减多级的统计,显示统计某一层次结构的聚合。
select device_id,os_id,app_id,client_version,from_id,count(user_id)
from test_xinyan_reg
group by device_id,os_id,app_id,client_version,from_id with rollup;
select
id
,name
,count(age)
from (
select 1 as id, 'a' as name,11 as age union all
select 2 as id, 'b' as name,12 as age union all
select 3 as id, 'c' as name,13 as age union all
select 4 as id, 'd' as name,14 as age union all
select 4 as id, 'd' as name,15 as age union all
select 4 as id, 'd' as name,16 as age union all
select 4 as id, 'd' as name,17 as age union all
select 4 as id, 'd' as name,18 as age
) t1
group by
id
,name
with cube
;
+------------+------------+------------+
| id | name | _c2 |
+------------+------------+------------+
| NULL | NULL | 8 |
| NULL | a | 1 |
| NULL | b | 1 |
| NULL | c | 1 |
| NULL | d | 5 |
| 1 | NULL | 1 |
| 1 | a | 1 |
| 2 | NULL | 1 |
| 2 | b | 1 |
| 3 | NULL | 1 |
| 3 | c | 1 |
| 4 | NULL | 5 |
| 4 | d | 5 |
+------------+------------+------------+ select
id
,name
,count(age)
from (
select 1 as id, 'a' as name,11 as age union all
select 2 as id, 'b' as name,12 as age union all
select 3 as id, 'c' as name,13 as age union all
select 4 as id, 'd' as name,14 as age union all
select 4 as id, 'd' as name,15 as age union all
select 4 as id, 'd' as name,16 as age union all
select 4 as id, 'd' as name,17 as age union all
select 4 as id, 'd' as name,18 as age
) t1
group by
id
,name
with rollup
;
+------------+------------+------------+
| id | name | _c2 |
+------------+------------+------------+
| NULL | NULL | 8 |
| 1 | NULL | 1 |
| 1 | a | 1 |
| 2 | NULL | 1 |
| 2 | b | 1 |
| 3 | NULL | 1 |
| 3 | c | 1 |
| 4 | NULL | 5 |
| 4 | d | 5 |
+------------+------------+------------+
ref: https://blog.csdn.net/qq_31573519/article/details/89054136
Hive高阶聚合函数 GROUPING SETS、Cube、Rollup的更多相关文章
- SQL Server ->> GROUPING SETS, CUBE, ROLLUP, GROUPING, GROUPING_ID
在我们制作报表的时候常常需要分组聚合.多组聚合和总合.如果通过另外的T-SQL语句来聚合难免性能太差.如果通过报表工具的聚合功能虽说比使用额外的T-SQL语句性能上要好很多,不过不够干脆,还是需要先生 ...
- grouping sets,cube,rollup,grouping__id,group by
例1: hive -e" select type ,status ,count(1) from usr_info where pt='2015-09-14' group by type,st ...
- [Hive_11] Hive 的高级聚合函数
0. 说明 Hive 的高级聚合函数 union all | grouping sets | cube | rollup pv //page view 页面访问量 uv //user view 访问人 ...
- 转:GROUPING SETS、ROLLUP、CUBE
转:http://blog.csdn.net/shangboerds/article/details/5193211 大家对GROUP BY应该比较熟悉,如果你感觉自己并不完全理解GROUP BY,那 ...
- GROUPING SETS、ROLLUP、CUBE
大家对GROUP BY应该比较熟悉,如果你感觉自己并不完全理解GROUP BY,那么本文不适合你.还记得当初学习SQL的时候,总是理解不了GROUP BY的作用,经过好长时间才终于明白GROUP BY ...
- SQL Server 之 GROUP BY、GROUPING SETS、ROLLUP、CUBE
1.创建表 Staff CREATE TABLE [dbo].[Staff]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [Money] [int] NULL, [Cr ...
- hive group by聚合函数增强
1.grouping sets grouping sets子句都可以根据UNION连接的多个GROUP BY查询进行逻辑表示 SELECT a,b,SUM(c)FROM tab1 GROUP BY a ...
- Hive 高阶应用开发示例(一)
Hive的一些常用的高阶开发 内容 1.开窗函数 2.行转列,列转行,多行转一行,一行转多行 3.分组: 增强型group 4.排序 5.关联 本次的内容: 内容1 和内容2,采用 ...
- Grouping Sets:CUBE和ROLLUP从句
在上一篇文章里我讨论了SQL Server里Grouping Sets的功能.从文中的例子可以看到,通过简单定义需要的分组集是很容易进行各自分组.但如果像从所给的列集里想要有所有可能的分布——即所谓的 ...
随机推荐
- XML之基础和DTD解析
本笔记可根据W3school教程学习: 首先-----了解XML文档结构.语法规范.作用 -----了解DTD约束的作用.具体约束语法 <?xml version="1.0" ...
- 阿里云SaaS生态战略发布:成就亿级营收独角兽
导语:本文中,阿里云智能资深技术专家黄省江从“势”“道”“术”三个方面分享了自己对于SaaS生态的理解,并介绍了SaaS加速器发布以来在产品.技术和商业侧最新的一些进展. 在321北京峰会上,阿里云公 ...
- 【python之路16】lambda表达式
1.lambda表达式,实际是建立一个简易的函数 下面代码中f1和f2表示是相同过程的代码 def f1(args): return args f2 = lambda args:args print( ...
- laravel-- 在laravel操作redis数据库的数据类型(string、哈希、无序集合、list链表、有序集合)
安装redis和连接redis数据库 在controller头部引入 一.基本使用 public function RedisdDbOne() { // 清空Redis数据库 Redis::flush ...
- Android中使用ormlite实现持久化--HelloOrmLite
Android中内置了sqlite,但是常用的开发语言java是面向对象的,而数据库是关系型的,二者之间的转化每次都很麻烦(主要是我对sql语言不熟悉).而Java Web开发中有很多orm框架,但是 ...
- UE4 Pak 相关知识总结
转载自:https://arcecho.github.io/2017/07/02/UE4-Pak-%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86%E6%80%BB%E7%BB ...
- 微服务开源生态报告 No.6
「微服务开源生态报告」,汇集各个开源项目近期的社区动态,帮助开发者们更高效的了解到各开源项目的最新进展. 社区动态包括,但不限于:版本发布.人员动态.项目动态和规划.培训和活动. 非常欢迎国内其他微服 ...
- JavaScript--封装好的运动函数+旋转木马例子
封装好的运动函数: 1.能控制目标元素的多种属性 2.能自动获取元素的样式表: 3.获取样式函数兼容 4.能对于元素的多属性进行动画(缓动动画)修改 5.能区分透明度等没单位的属性和px属性的变化 a ...
- git安装方法
点击 http://git-scm.com/download 选择你使用的操作系统,如果是linux就可以看到命令行,windows则是下载客户端
- MSSQL 为db创建user
use [IBatisNet]GO if not exists (select * from master.dbo.syslogins where loginname = N'IBatisNet')B ...