每个用户截止到每月为止的最大单月访问次数和累计到该月的总访问次数
0: jdbc:hive2:
. . . . . . . . . . . . . . > name string,
. . . . . . . . . . . . . . > time string,
. . . . . . . . . . . . . . > views int)
. . . . . . . . . . . . . . > row format delimited fields terminated by ',';
0: jdbc:hive2:
# 数据
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
select
name,time,
max(month_views) over(partition by name order by time rows between unbounded preceding and current row) max_views,
sum(month_views) over(partition by name order by time rows between unbounded preceding and current row) month_view_add,
month_views
from
(select name,time,sum(views) month_views from view_log group by name,time) tb1