hive 开窗函数
作者:互联网
-- 开窗------------------------分组排序--------------------------------- -- row_number() over(partition by ... order by ...) 组内的排名序号 1 2 3 4 5 6 7 -- rank() over(partition by userid order by pv desc) 组内的排名序号 1 2 3 3 5 6 6 8 -- dense_rank over(partition by userid order by pv desc) 组内的排名序号 1 2 3 3 4 5 6 6 7
如果不添加partition by,就是不分组,rank() over(order by pv desc)就是对全部数据,用pv值进行排序 -- 开窗函数:SUM,MAX,MIN, AVG -- SUM() over(partition by ... order by ... rows between ... and ...) -- MAX() over(partition by ... order by ... rows between ... and ...) -- MIN() over(partition by ... order by ... rows between ... and ...) -- AVG() over(partition by ... order by ... rows between ... and ...) -- 其中rows between ... and ...
从第一行开始累加到当前行 :
默认策略: sum(pv) over(partition by userid order by createtime)
使用between:sum(pv) over(partition by userid order by createtime rows between unbounded preceding and current row)
从向上推3行 累加到当前行 : sum(pv) over(partition by userid order by createtime rows between 3 preceding and current row) 从向上推3行 累加到向后推一行 : sum(pv) over(partition by userid order by createtime rows between 3 preceding and 1 following) 从当前行累加到最后一行 : sum(pv) over(partition by userid order by createtime rows between current row and unbounded following) 从向上推3行 累加到最后一行 : sum(pv) over(partition by userid order by createtime rows between 3 preceding and unbounded following) 从向上推3行,到最后一行 求最大值: max(pv) over(partition by userid order by createtime rows between 3 preceding and unbounded following) 从向上推3行,到最后一行 求最小值 : min(pv) over(partition by userid order by createtime rows between 3 preceding and unbounded following) 从向上推3行,到最后一行 求平均值 : avg(pv) over(partition by userid order by createtime rows between 3 preceding and unbounded following) -- lag 将上一行的数据和本行数据放在同一行:LAG(createtime,1,'1970-01-01') OVER(PARTITION BY userid ORDER BY createtime) 将上上一行的数据和本行数据放在同一行:LAG(createtime,2,'1970-01-01') OVER(PARTITION BY userid ORDER BY createtime) -- lead 将下一行的数据和本行数据放在同一行:LEAD(createtime,1,'1970-01-01') OVER(PARTITION BY userid ORDER BY createtime) 将下下一行的数据和本行数据放在同一行:LEAD(createtime,2,'1970-01-01') OVER(PARTITION BY userid ORDER BY createtime)
标签:...,函数,over,partition,userid,hive,order,开窗,createtime 来源: https://www.cnblogs.com/ljc1212/p/16368150.html