Hive开窗

练习的时候发现hive也支持开窗函数

三个字段的意思:
用户名,月份,访问次数
 
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 Jan-15 33 33 33
A Feb-15 33 43 10
A Mar-15 38 81 38
B Jan-15 30 30 30
B Feb-15 30 45 15
B Mar-15 44 89 44
B Mar-15 44 89 44

用这个题正好验证了开窗的使用方法与Oracle一样,这就很爽了!!!

select t.name,t.d,t.times as thismonth,
sum(t.times)over(partition by t.name order by t.d rows between  unbounded preceding and  current row ) sums,
max(t.times)over(partition by t.name order by t.d rows between unbounded preceding and current row) max 
from(select t.name,t.t_date d,sum(t.times) times From t_hive01 t group by t.name,t.t_date)t;

猜你喜欢

转载自blog.csdn.net/weixin_44033089/article/details/86545361