Hive增强聚合函数(group sets、cube、rollup)

/*
增强聚合函数group sets
group sets是一种将多个group by逻辑写在一个sql语句中的便利写法,等价于将不同维度的group by结果集进行union all

2015-03,2015-03-10,cookie1
2015-03,2015-03-10,cookie5
2015-03,2015-03-12,cookie7
2015-04,2015-04-12,cookie3
2015-04,2015-04-13,cookie2
2015-04,2015-04-13,cookie4
2015-04,2015-04-16,cookie4
2015-03,2015-03-10,cookie2
2015-03,2015-03-10,cookie3
2015-04,2015-04-12,cookie5
2015-04,2015-04-13,cookie6
2015-04,2015-04-15,cookie3
2015-04,2015-04-15,cookie2
2015-04,2015-04-16,cookie1
*/
create table cookie_info
(
    month     string,
    day       string,
    cookie_id string
) row format delimited fields terminated by ',';

load data local inpath '/root/hivedata/cookie_info.txt' overwrite into table cookie_info;

select *
from cookie_info;

select month,
       day,
       count(distinct cookie_id) num,
       grouping__id -- 表示结果属于哪一个分组集合
from cookie_info
group by month, day
    grouping sets ( month, day)
order by grouping__id;

-- 等价于
select month,
       null as                   day,
       count(distinct cookie_id) num,
       1    as                   grouping__id
from cookie_info
group by month
union all
select null as                   month,
       day,
       count(distinct cookie_id) num,
       2    as                   grouping__id
from cookie_info
group by day;



select month,
       day,
       count(distinct cookie_id) num,
       grouping__id -- 表示结果属于哪一个分组集合
from cookie_info
group by month, day
    grouping sets ( month, day, ( month, day))
order by grouping__id;

-- 等价于
select month,
       null as                   day,
       count(distinct cookie_id) num,
       1    as                   grouping__id
from cookie_info
group by month
union all
select null as                   month,
       day,
       count(distinct cookie_id) num,
       2    as                   grouping__id
from cookie_info
group by day
union all
select month,
       day,
       count(distinct cookie_id) num,
       3 as                      grouping__id
from cookie_info
group by month, day;


/*
2.增强聚合函数cube
cube表示根据group by的维度的所有组合进行聚合
对于cube来说,如果有n个维度,则所有组合的总数为2^n
例如cube有a,b,c三个维度,则所有组合情况是:(a,b,c),(a,b),(a,c),(b,c),(a),(b),(c),()
*/
select month,
       day,
       count(distinct cookie_id) num,
       grouping__id -- 表示结果属于哪一个分组集合
from cookie_info
group by month, day
with cube
order by grouping__id;

-- 等价于
select null, null, count(distinct cookie_id) num, 0 as grouping__id
from cookie_info
union all
select month, null, count(distinct cookie_id) num, 1 as grouping__id
from cookie_info
group by month
union all
select null, day, count(distinct cookie_id) num, 2 as grouping__id
from cookie_info
group by day
union all
select month, day, count(distinct cookie_id) num, 3 as grouping__id
from cookie_info
group by month, day

/*
rollup是cube的子集,以最左侧的维度为主,从该维度进行层级聚合
比如rollup有a,b,c三个维度,则所有组合的情况是:(a,b,c),(a,b),(a),()
*/
-- 以month维度进行层级聚合
select month,
       day,
       count(distinct cookie_id) num,
       grouping__id -- 表示结果属于哪一个分组集合
from cookie_info
group by month, day
with rollup
order by grouping__id;

-- 将month和day调换顺序,则以day维度进行聚合
select day,
       month,
       count(distinct cookie_id) num,
       grouping__id -- 表示结果属于哪一个分组集合
from cookie_info
group by day, month
with rollup
order by grouping__id;
### 使用 Hive 中 `GROUPING SETS` 的方法 #### 示例与语法解释 在 Hive SQL 查询中,`GROUPING SETS` 提供了一种简洁的方式来执行多维分组聚合。这使得可以在单个查询中指定多个不同的组合来进行数据汇总。 创建表并插入测试数据: ```sql CREATE TABLE IF NOT EXISTS student_scores ( class STRING, sex STRING, course STRING, score INT ); INSERT INTO student_scores VALUES ('ClassA', 'Male', 'Math', 80), ('ClassB', 'Female', 'English', 90), ('ClassA', 'Female', 'Math', 75); ``` 使用 `GROUPING SETS` 执行复杂分组操作: ```sql SELECT GROUPING__ID, -- 特殊列用于区分不同层次的分组结果 class, sex, course, AVG(score) AS average_score FROM student_scores GROUP BY class, sex, course WITH ROLLUP; -- 或者使用 CUBE 替代 ROLLUP 来获取更全面的结果集 ``` 上述例子展示了如何利用 `WITH ROLLUP` 关键字来简化某些类型的 `GROUPING SETS` 定义[^1]。对于更加复杂的场景,则可以直接定义具体的分组集合: ```sql SELECT GROUPING__ID, class, sex, course, AVG(score) AS average_score FROM student_scores GROUP BY class, sex, course GROUPING SETS( (class, course), (class, sex), (sex, course), () -- 表示全局总计行 ); ``` 此查询会返回四类不同的分组统计信息:按班级和课程、按性别和课程、仅限于特定科目以及整个表格的整体平均成绩[^3]。 注意,在输出结果集中有一个名为 `GROUPING__ID` 的特殊字段,它用来指示当前记录对应的是哪种级别的分组情况。通过检查该值可以帮助理解每一行代表的数据范围[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值