hive窗口函数使用详解
生活随笔
收集整理的這篇文章主要介紹了
hive窗口函数使用详解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ntile
- 用于將分組數(shù)據(jù)按照順序切分成n片,返回當(dāng)前記錄所在的切片值。
- 經(jīng)常用來取前30% 帶有百分之多少比例的記錄什么的
注意:
NTILE不支持ROWS BETWEEN,比如 NTILE(2) OVER(PARTITION BY id ORDER BY mod_date ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)
實(shí)例:
select *,ntile(3) over(partition by id order by mod_date) from t0411;collect_set
求多行的集合
實(shí)例:
select id, mod_date,src,cur,row_number() over(partition by id order by mod_date asc) as num,--使集合有序collect_set(src) over(partition by id order by mod_date asc ) as srcs1,collect_set(cur) over(partition by id ) as srcs2 from db01.t0411rows
rows函數(shù):
current row:當(dāng)前行
n PRECEDING:往前n行
n FOLLOWING:往后n行
UNBOUNDED:起點(diǎn)
UNBOUNDED PRECEDING:從前面起點(diǎn)
UNBOUNDED FOLLOWING:到后面終點(diǎn)
LAG(col,n):往前的第n行
LEAD(col,n):往后的第n行
實(shí)例1:
-- 計(jì)算每個(gè)id從開始到當(dāng)前時(shí)間的總的修改次數(shù) -- 參數(shù)講解 -- partition by id:按照id分組 -- order by mod_date:按照日期進(jìn)行排序 -- UNBOUNDED PRECEDING:從起點(diǎn)開始 -- CURRENT ROW:到當(dāng)前行 select *,count(id) over(partition by id order by mod_date rows between UNBOUNDED PRECEDING and CURRENT ROW) from t0411;實(shí)例2:
與collet_set實(shí)例效果一樣,但更簡潔
實(shí)例2:
-- 計(jì)算相鄰三行的值(第一行計(jì)算當(dāng)前行 + 后一行; 最后一行計(jì)算當(dāng)前行 + 前一行) -- 參數(shù)講解 -- order by mod_date:按照日期進(jìn)行排序 -- 1 preceding:當(dāng)前行的前1行 -- 1 following:當(dāng)前行的后一行 select *,count(id) over(order by mod_date rows between 1 preceding and 1 following) from t0411;
實(shí)例3:
測試數(shù)據(jù)見另一篇博客
總結(jié)
以上是生活随笔為你收集整理的hive窗口函数使用详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: excel解决线性规划求解问题
- 下一篇: 思科OSPF详细配置命令过程