Hive中窗口函数的使用

准备测试表

CREATE TABLE over_test(name STRING, month int,cnt int) row format delimited fields Terminated by ',' lines terminated by '\n' STORED AS textfile;

LOAD DATA LOCAL INPATH '/opt/data/over_test.txt' INTO TABLE over_test;

数据

夏明,1,10
夏明,2,20
夏明,3,30
夏明,4,40
夏明,5,50
夏明,6,60
夏明,7,70
夏明,8,80
夏明,9,90
夏明,10,100
夏明,11,30
夏明,12,60
无言,1,20
无言,2,10
无言,3,50
无言,4,70
无言,5,60
无言,6,70
无言,7,30
无言,8,10
无言,9,20
无言,10,300
无言,11,200
无言,12,888

分组排序


select * ,
row_number() over(partition by name order by cnt asc) as rmp,
rank() over(partition by name order by cnt asc) as rp,
dense_rank() over(partition by name order by cnt asc) as drp 
 from over_test;


分组累计求和


select * ,
sum(cnt) over(partition by name order by month asc) as sum_cnt 
from over_test;


分组求平均数


select * ,
avg(cnt) over(partition by name order by month asc) as avg_cnt 
from over_test;


分组滑动


select * ,
sum(cnt) over(partition by name order by month asc rows between 1 preceding and 1 following) as sum_cnt ,
avg(cnt) over(partition by name order by month asc rows between 1 preceding and 1 following) as avg_cnt 
from over_test;

 

参考官网资料

LanguageManual WindowingAndAnalytics - Apache Hive - Apache Software Foundation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值