-- 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的更多相关文章

  1. SQL Server ->> GROUPING SETS, CUBE, ROLLUP, GROUPING, GROUPING_ID

    在我们制作报表的时候常常需要分组聚合.多组聚合和总合.如果通过另外的T-SQL语句来聚合难免性能太差.如果通过报表工具的聚合功能虽说比使用额外的T-SQL语句性能上要好很多,不过不够干脆,还是需要先生 ...

  2. 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 ...

  3. [Hive_11] Hive 的高级聚合函数

    0. 说明 Hive 的高级聚合函数 union all | grouping sets | cube | rollup pv //page view 页面访问量 uv //user view 访问人 ...

  4. 转:GROUPING SETS、ROLLUP、CUBE

    转:http://blog.csdn.net/shangboerds/article/details/5193211 大家对GROUP BY应该比较熟悉,如果你感觉自己并不完全理解GROUP BY,那 ...

  5. GROUPING SETS、ROLLUP、CUBE

    大家对GROUP BY应该比较熟悉,如果你感觉自己并不完全理解GROUP BY,那么本文不适合你.还记得当初学习SQL的时候,总是理解不了GROUP BY的作用,经过好长时间才终于明白GROUP BY ...

  6. SQL Server 之 GROUP BY、GROUPING SETS、ROLLUP、CUBE

    1.创建表 Staff CREATE TABLE [dbo].[Staff]( ,) NOT NULL, ) NULL, ) NULL, ) NULL, [Money] [int] NULL, [Cr ...

  7. hive group by聚合函数增强

    1.grouping sets grouping sets子句都可以根据UNION连接的多个GROUP BY查询进行逻辑表示 SELECT a,b,SUM(c)FROM tab1 GROUP BY a ...

  8. Hive 高阶应用开发示例(一)

    Hive的一些常用的高阶开发 内容    1.开窗函数   2.行转列,列转行,多行转一行,一行转多行   3.分组: 增强型group   4.排序  5.关联 本次的内容: 内容1 和内容2,采用 ...

  9. Grouping Sets:CUBE和ROLLUP从句

    在上一篇文章里我讨论了SQL Server里Grouping Sets的功能.从文中的例子可以看到,通过简单定义需要的分组集是很容易进行各自分组.但如果像从所给的列集里想要有所有可能的分布——即所谓的 ...

随机推荐

  1. Python之路,Day2 - Python基础(转载Alex)

    Day2-转自金角大王 本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存 ...

  2. git中的错误

    ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@gitee.co ...

  3. html转换成pdf

    指定html转换成pdf 安装插件: npm install --save html2canvas npm install jspdf --save 引入 plugins/ htmlToPdf.js ...

  4. ubuntu设置终端命令历史记录

    ----------------------------------------------- HISTTIMEFORMAT='%F %T ' # 使用HISTTIMEFORMAT在历史中显示TIME ...

  5. spring源码学习之默认标签的解析(二)

    这个是接着上一篇来写,主要是这章内容比较多,还是分开来写吧! 一.AbstractBeanDefinition属性介绍 XML中的所有的属性都可以在GenericBeanDefinition中找到对应 ...

  6. SQL Server导入数据报错"无法在只读列“Id”中插入数据"

    使用sql server 导入数据报错:无法在只读列'id'中插入数据.如下图所示: 查找出现该问题的原因是表中id为自动增长的标识列,需要在[编辑映射]中勾选"启用标识插入": ...

  7. LintCode_372 在O(1)时间复杂度删除链表节点

    题目 给定一个单链表中的表头和一个等待被删除的节点(非表头或表尾).请在在O(1)时间复杂度删除该链表节点.并在删除该节点后,返回表头. 思路 因为O(1)内删除所以 不能从头节点去遍历找他的前驱节点 ...

  8. SQL Server2008 卸载

    先把SQL Server卸载,再把安装时产生的“Microsoft SQL Server”文件夹删掉,在运行注册表,把HKEY_CURRENT_USER\Software\Microsoft\Micr ...

  9. Windows系统MySQL8.0的安装教程

    MySQL推出的8.0版本亮点多多,尤其是两倍的提速更让我们迫不及待地安装一睹为快.然而目前我们所用的版本还在5.7之前,今天小编给家分享一下MySQL8.0的安装,尤其是多版本MySQL的共存. 方 ...

  10. Elasticsearch 启动需要密码?

    vagrant@homestead:~$ systemctl disable elasticsearch.service Synchronizing state of elasticsearch.se ...