工作中,我们经常会union all很多表数据,但你要是还在憨憨的把所有union all都写出来可就上天了。今天来给大家介绍一种HQL骚操作。省时省力,简单的一批。
hive 函数:grouping set
格式:
select from table --选取字段
group by --声明参与group by的字段有哪些
grouping sets ( (), (), (), () ) --group by组合
简单来说,grouping set就是一种将多个group by逻辑写到一个sql中
举个栗子:
select a, b, c, sum(d) from table
group by a, b, c
grouping sets ( (a, b, c), (a, b), (a, c), (b, c) )
等价于
select a,b,c sum(d) from table group by a, b, c
union all
select a,b sum(d) from table group by a, b
union all
select a,c sum(d) from table group by a, c
union all
select b,c sum(d) from table group by b, c
这tm也太骚了,起飞!!!