海牛部落 hive 系列教程(二十三):hive 的 grouping sets、排序、窗口函数用法

教程 青牛 ⋅ 于 2017-06-05 18:00:05 ⋅ 最后回复由 wlee 2022-04-01 15:33:31 ⋅ 15688 阅读

1. GROUPING SETS

语法
SELECT a, b, SUM( c ) FROM tab1 GROUP BY a, b GROUPING SETS ( (a, b), a, b, () )
等于
SELECT a, b, SUM( c ) FROM tab1 GROUP BY a, b
UNION ALL
SELECT a, null, SUM( c ) FROM tab1 GROUP BY a
UNION ALL
SELECT null, b, SUM( c ) FROM tab1 GROUP BY b
UNION ALL
SELECT null, null, SUM( c ) FROM tab1

示例
select country,gpcategory,count(1) from user_install_status_mid where dt=20141228
group by country,gpcategory
grouping sets((country,gpcategory),(country),(gpcategory),()) limit 10;
等于
select * from (
select country,gpcategory,count(1) from user_install_status_mid where dt=20141228
group by country,gpcategory
union all
select country,null,count(1) from user_install_status_mid where dt=20141228
group by country
union all
select null,gpcategory,count(1) from user_install_status_mid where dt=20141228
group by ,gpcategory
union all
select null,null,count(1) from user_install_status_mid where dt=20141228
) a limit 10;
file

以下内容回帖可见………………

回复帖子,然后刷新页面即可查看隐藏内容

版权声明:原创作品,允许转载,转载时务必以超链接的形式表明出处和作者信息。否则将追究法律责任。来自海汼部落-青牛,http://hainiubl.com/topics/118
回复数量: 47
暂无评论~~
  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
  • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
Ctrl+Enter