Hive高级聚合函数

0、基础知识

(1)pv:page view(页面访问量)

(2)uv:user view(访问人数)

(3)uv表的数据如下

Hive高级聚合函数_第1张图片

(4)统计每个月的用户浏览量,"distinct"关键字是去除重复的值

select month, count(distinct id) from uv group by month;

Hive高级聚合函数_第2张图片

1、union all:表联合操作

eg:统计每天和每月的用户访问量

select month, count(distinct id) from uv group by month union all select day, count(distinct id) from uv group by day;

Hive高级聚合函数_第3张图片

2、grouping sets:只统计指定字段

eg:统计每天和每月的用户访问量,grouping__id是分组的组号(两个下划线)

select month, day, count(distinct id), grouping__id from uv group by month, day grouping sets(month, day);

Hive高级聚合函数_第4张图片

3、with cube:统计指定字段的所有组合(包括NULL)

eg:统计每天和每月的用户访问量,grouping__id是分组的组号(两个下划线)

select month, day, count(distinct id), grouping__id from uv group by month, day with cube order by grouping__id;

Hive高级聚合函数_第5张图片

4、with rollup:逐层统计指定字段

eg:统计每天和每月的用户访问量,grouping__id是分组的组号(两个下划线),组号扔按所有组合排序,但只显示逐层统计的记录

select month, day, count(distinct id), grouping__id from uv group by month, day with rollup order by grouping__id;

Hive高级聚合函数_第6张图片

 

你可能感兴趣的:(Hive高级聚合函数)