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的功能.从文中的例子可以看到,通过简单定义需要的分组集是很容易进行各自分组.但如果像从所给的列集里想要有所有可能的分布——即所谓的 ...
随机推荐
- Java处理正则验证手机号-详解
参考博客:https://www.cnblogs.com/wangzn/p/7212587.html https://www.cnblogs.com/go4mi/p/6426215.html pack ...
- JS中的$符号
1. 首先可以用来表示变量, 比如变量 var s='asdsd'或var $s='asdasd'; 2. 在正则表达式中,它可以匹配结尾 /sa$/.test(string) 匹配string字符串 ...
- homebrew长时间停在Updating Homebrew 这个步骤
在国内的网络环境下使用 Homebrew 安装软件的过程中可能会长时间卡在 Updating Homebrew 这个步骤. 例:执行 brew install composer 命令 ➜ ~ brew ...
- Linux ifconfig 查看网络接口状态
Linux ifconfig 如果不接任何参数,就会输出当前网络接口的情况: [root@localhost ~]# Linux ifconfig eth0 Link encap:Ether ...
- 洛谷P1052 过河
P1052 过河 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上. 由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青 ...
- 阿里云出手SaaS生态,中国SaaS市场小而不强有望破解
企业服务SaaS市场还有很大的增长空间.SaaS的鼻祖Salesforces今年5月迈上了千亿美元市值的门槛,再一次为ToB市场注入了兴奋剂.单单一个SaaS CRM,目前全球的市场规模就超过400亿 ...
- Vue--moment时间格式插件安装和使用
使用链接:http://momentjs.cn/ 1.安装monent 2.导入 3.过滤器 4.template模板使用:
- python findall函数
- 洛谷P1929 迷之阶梯
P1929 迷之阶梯 题目描述 在经过地球防卫小队的数学家连续多日的工作后,外星人发的密码终于得以破解.它 告诉我们在地球某一处的古老遗迹中,存在有对抗这次灾难的秘密武器.防卫小队立即赶 到这处遗迹. ...
- 阿里云CDN边缘脚本EdgeScript公测:简单语法完成CDN复杂配置
CDN可以将源站内容分发至最靠近用户侧的节点,使得用户就近获取内容,提高用户的访问成功率和效率.作为CDN运维工程师,他的日常工作就是通过CDN系统的配置和管理,来确保CDN业务正常运转,以此来保障网 ...