-- 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. [jnhs]使用netbeans生成的webapp发布到tomcat是需要改名字的,不然就是404Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

    2018-12-21更新 退出tomcat然后删除解压之后的文件夹,然后再启动tomcat也可以解决(安装版tomcat) 2018-12-9更新 有时候这样也可以解决 第一次使用tomcat发布we ...

  2. jnhs中国省市县区mysql数据表不带gps坐标

    1.查省 SELECT * FROM china WHERE china.Pid=0 2.查市 SELECT * FROM chinaWHERE china.Pid=330000 3.查区 SELEC ...

  3. 使用Spring Cache + Redis + Jackson Serializer缓存数据库查询结果中序列化问题的解决

    应用场景 我们希望通过缓存来减少对关系型数据库的查询次数,减轻数据库压力.在执行DAO类的select***(), query***()方法时,先从Redis中查询有没有缓存数据,如果有则直接从Red ...

  4. 基于Kebernetes 构建.NET Core技术中台

    原文:基于Kebernetes 构建.NET Core技术中台 我们为什么需要中台 我们现在处于企业信息化的新时代.为什么这样说呢? 过去企业信息化的主流重心是企业内部信息化.但现在以及未来的企业信息 ...

  5. html5的离线存储问题集合

    HTML5的离线存储使用一个manifest文件来标明哪些文件是需要被存储的,使用如 来引入一个manifest文件,这个文件的路径可以是相对的,也可以是绝对的,如果你的web应用很多,而且希望能集中 ...

  6. Vbulletin Used to Show Malicious Advertisements

    In the past, we have seen a massive amount of vBulletin websites compromised through theVBSeo Vulner ...

  7. 利用Git搭建自动部署的Laravel环境 - 钟晨宇的博客 - CSDN博客

    目标:服务器上搭建Laravel环境,本地使用IDE进行开发,使用Homestead做本地调试环境,代码提交后自动部署到服务器Root目录下. 下面是整个流程的示意图:  1. 准备工作,搭建LNMP ...

  8. django 结合 bootstrap 使用

    git clone https://github.com/dyve/django-bootstrap3.git 要运行demo,需要在demo 中为其增加一个符号链接 bootstrap3 到上层目录 ...

  9. 在vue项目中同时使用element-ui和mint-ui,的时候,.babelrc配置文件怎么写

    我们安装vue组件库的时候,考虑到大小问题,需要根据需要仅引入部分组件 借助 babel-plugin-component,我们可以只引入需要的组件,以达到减小项目体积的目的. 但是在配置  .bab ...

  10. 用JS实线放大镜的效果

    今天花了点时间,复习了下使用原生JS实线放大镜的效果.在制作过程中,也是很到了一些问题,在这里总结下. HTML代码如下: <div id="preview"> < ...