欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

hive高级分组聚合groupingsets,cube,rollup

时间:2023-06-24

        日常使用较少,主要记录grouping sets,cube,rollup这三个分组聚合。

        首先,使用高级分组聚合的语法时,要注意hive是否开启了向量模式。

        set hive.verctorized.execution.enabled = true;

1、grouping sets

select prov_id ,deep ,count(1) as numfrom dim.dim_citywhere prov_id = 110000and deep = 1group by prov_id ,deepgrouping sets( (prov_id,deep) ,prov_id ,deep );--等同于select prov_id ,deep ,count(1) as numfrom dim.dim_citywhere prov_id = 110000and deep = 1group by prov_id ,deepunion allselect prov_id ,null ,count(1) as numfrom dim.dim_citywhere prov_id = 110000and deep = 1group by prov_idunion allselect null ,deep ,count(1) as numfrom dim.dim_citywhere prov_id = 110000and deep = 1group by deep

2、cube

select prov_id ,deep ,count(1) as numfrom dim.dim_citywhere prov_id = 110000and deep = 1group by prov_id ,deepwith cube;--等同于select prov_id ,deep ,count(1) as numfrom dim.dim_citywhere prov_id = 110000and deep = 1group by prov_id ,deepgrouping sets ((prov_id,deep) ,prov_id ,deep ,());

3、rollup

select prov_id ,deep ,count(1) as numfrom dim.dim_citywhere prov_id = 110000and deep = 1group by prov_id ,deepwith rollup;--等同于select prov_id ,deep ,count(1) as numfrom dim.dim_citywhere prov_id = 110000and deep = 1group by prov_id ,deepgrouping sets ((prov_id,deep) ,prov_id ,());

如果确实有比较契合的需求用起来还是比较方便的,但是使用场景确实也少

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。