Hive学习笔记(7)—— hive实战 级联求和

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012292754/article/details/82390691

1 数据表

访客访问次数统计表 t_access_times.txt

A,2015-01,5
A,2015-01,15
B,2015-01,5
A,2015-01,8
B,2015-01,25
A,2015-01,5
A,2015-02,4
A,2015-02,6
B,2015-02,10
B,2015-02,5

2 新建表并且导入数据

这里写图片描述
这里写图片描述

3 测试

3.1 求每个用户每个月的访问次数

select username,month,sum(times) as times from t_access_times group by username,month;
这里写图片描述

3.2 将月访问次数表自己join

select A.*,B.* FROM
 (select username,month,sum(times) as times from t_access_times group by username,month)A
inner join
(select username,month,sum(times) as times from t_access_times group by username,month)B
on A.username=B.username;

这里写图片描述

3.3 从上一步的结果中进行分组字段查询

分组字段: a.username, a.month
求月累积值: 将 b.month <= a.month 所有 b.times 求和

select A.username,A.month,max(A.times) as times,sum(B.times) as accumulate
from 
(select username,month,sum(times) as times from t_access_times group by username,month) A 
inner join 
(select username,month,sum(times) as times from t_access_times group by username,month) B
on
A.username=B.username
where B.month <= A.month
group by A.username,A.month
order by A.username,A.month;

这里写图片描述

猜你喜欢

转载自blog.csdn.net/u012292754/article/details/82390691