一、简介
根据不同的维度组合进行聚合,等价于将不同维度的GROUP BY结果集进行UNION ALL。
二、grouping sets的基本使用
Presto 的group by后面不能加字段,hive则反之
2.1 hive写法
select
month,
day,
count(userid)
from test.t_user
group by day,month
grouping sets(month,day,(month,day));
2.2 Presto 写法
select
month,
day,
count(userid)
from test.t_user
group by
grouping sets(month,day,(month,day));
三、grouping的使用
3.1.hive写法
select
month,
day,
count(userid), -- 0 0
case when grouping__id = 3 -- month_day 1 1
then 'month_day'
when grouping__id = 2 -- month 1 0
then 'month'
when grouping__id = 1 -- day 0 1
then 'day'
when grouping__id = 0 -- all 0 0
then 'all'
end as group_type
from test.t_user
group by day,month -- 这里的顺序要和select后边的字段顺序相反
grouping sets (month,day,(month,day),());
结果如下:
3.2.Presto写法
select month, day, count (userid), case
when grouping (month, day) = 1
then 'month'
when grouping (month, day) = 2
then 'day'
when grouping (month, day) = 0
then 'month_day'
end group_type
from test.t_user
group by grouping sets (month, day, (month, day));
结果如下:
总结
Presto的语法和Hive的语法有一些区别,使用时需要注意,使用grouping sets推荐在Presto执行,执行速度更快,但在Presto写sql时它的语句提醒功能没有Hive那么健全,可以在Hive将sql写好,然后到Presto执行