题目: 每个用户截止到每月为止的最大单月访问次数和累计到该月的总访问次数: 用户名,月份,访问次数 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 A, 2015-03, 16 A, 2015-03, 22 B, 2015-03, 23 B, 2015-03, 10 B, 2015-03, 11 最后结果展示: 用户 月份 最大访问次数 总访问次数 当月访问次数 A 2015-01 33 33 33
A 2015-02 33 43 10 A 2015-03 38 81 38 B 2015-01 30 30 30 B 2015-02 30 45 15 B 2015-03 44 89 44 解题思路: 1>先求出每月的总访问次数
hive> create table hrT1 as select id, datetime as month, sum(times) as times from hrtest group by id,datetime;
2>自连,并以第一张表为主,实现求和、排序、分组
hive> select f.id,f.month,f.times,sum(s.times) sumtime,max(s.times) maxtime from hrT1 f inner join hrT1 s on f.id=s.id where f.month>=s.month group by f.id,f.month,f.times order by f.id,f.month;
转的网页