【博学谷学习记录】超强总结,用心分享 | 狂野大数据-聚合函数增强--grouping sets


一、简介

根据不同的维度组合进行聚合,等价于将不同维度的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执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值