-- 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. Jboss 默认加载项目访问

    修改JBOSS的server.xml路径为: D:\Program Files\jboss-4.2.2.GA\server\default\deploy\jboss-web.deployer\serv ...

  2. day 52

    目录 静态文件配置 form表单默认是get请求 request方法的初识 数据库的迁移命令 静态文件配置 默认情况下所有的html文件都放在templates文件夹内 什么是静态文件 网站所使用到的 ...

  3. CentOS7配置中文支持与部署GitLab服务器

    给你的 CentOS 7 安装中文支持 1.首先需要中文字体以便支持命令行终端的中文显示需求: yum groupinstall "fonts" 碰到提示输入 y 回车继续安装,大 ...

  4. select2 如何自定义提示信息-布布扣-bubuko.com

    标签:color   dom   layui   href   默认事件   替换   each   ase   options 最近项目中使用到了select2来美化和丰富下拉框的功能,这款插件功能 ...

  5. excel一些常用的函数

    函数分类: 关联匹配类 清洗处理类 逻辑运算类 计算统计类 时间序列类 一.关联匹配类 经常性的,需要的数据不在同一个excel表或同一个excel表不同sheet中,数据太多,copy麻烦也不准确, ...

  6. Hdu 1045 二分匹配

    题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. 【JZOJ5094】【GDSOI2017第四轮模拟day3】鸽子 计算几何+floyd

    题面 养鸽人要监视他的鸽子,有n只鸽子站在平面上,他可以在m个给定的点上设置监视器,如果一只鸽子在某个监视器上或者在两个监视器所连直线上或者在三个监视器所连直线的三角形内则其就咕咕咕了,现在养鸽人要让 ...

  8. Linux下的权限管理

    Linux系统上对文件的权限有着严格的控制,用于如果相对某个文件执行某种操作,必须具有对应的权限方可执行成功. Linux下文件的权限类型一般包括读,写,执行.对应字母为 r.w.x. Linux下权 ...

  9. I / O流 类

    一.概述 1 基本概念 I/O就是Input/Output的简写,也就是输入/输出的含义. I/O流就是指像流水一样源源不断地进行读写的过程.   2 基本分类   根据读写数据的单元分为:字节流 和 ...

  10. [leetcode] Reverse Words in a String [1]

    一.题目: Given an input string, reverse the string word by word. For example, Given s = "the sky i ...