關於Hive中經常使用函數須要注意的點小合集

1.COALESCE( value1,value2,... )
The COALESCE function returns the fist not NULL value from the list of values. If all the values in the list are NULL, then it returns NULL.
Example: COALESCE(NULL,NULL,5,NULL,4) returns 5;

coalesce()函數,在處理空值時,'' 和NULL是不同的,函數認爲NULL是空值,但'' 不是空值,
故coalesce('','test')會返回'',而不是test;而coalesce(NULL,'test')回返回test;sql

 

2.NTILE,ROW_NUMBER,RANK,DENSE_RANK

①NTILE:用戶將分組數據按照順序切分紅N片,返回當前切片值。segmentfault

NTILE(n),用於將分組數據按照順序切分紅n片,返回當前切片值
NTILE不支持ROWS BETWEEN,好比 NTILE(2) OVER(PARTITION BY cookieid ORDER BY createtime ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)
若是切片不均勻,默認增長第一個切片的分佈

SELECT 
cookieid,
createtime,
pv,
NTILE(2) OVER(PARTITION BY cookieid ORDER BY createtime) AS rn1,    --分組內將數據分紅2片
NTILE(3) OVER(PARTITION BY cookieid ORDER BY createtime) AS rn2,  --分組內將數據分紅3片
NTILE(4) OVER(ORDER BY createtime) AS rn3        --將全部數據分紅4片
FROM lxw1234 
ORDER BY cookieid,createtime;
 
cookieid day           pv       rn1     rn2     rn3
-------------------------------------------------
cookie1 2015-04-10      1       1       1       1
cookie1 2015-04-11      5       1       1       1
cookie1 2015-04-12      7       1       1       2
cookie1 2015-04-13      3       1       2       2
cookie1 2015-04-14      2       2       2       3
cookie1 2015-04-15      4       2       3       3
cookie1 2015-04-16      4       2       3       4
cookie2 2015-04-10      2       1       1       1
cookie2 2015-04-11      3       1       1       1
cookie2 2015-04-12      5       1       1       2
cookie2 2015-04-13      6       1       2       2
cookie2 2015-04-14      3       2       2       3
cookie2 2015-04-15      9       2       3       4
cookie2 2015-04-16      7       2       3       4

好比,統計一個cookie,pv數最多的前1/3的天
SELECT 
cookieid,
createtime,
pv,
NTILE(3) OVER(PARTITION BY cookieid ORDER BY pv DESC) AS rn 
FROM lxw1234;
 
--rn = 1 的記錄,就是咱們想要的結果
 
cookieid day           pv       rn
----------------------------------
cookie1 2015-04-12      7       1
cookie1 2015-04-11      5       1
cookie1 2015-04-15      4       1
cookie1 2015-04-16      4       2
cookie1 2015-04-13      3       2
cookie1 2015-04-14      2       3
cookie1 2015-04-10      1       3
cookie2 2015-04-15      9       1
cookie2 2015-04-16      7       1
cookie2 2015-04-13      6       1
cookie2 2015-04-12      5       2
cookie2 2015-04-14      3       2
cookie2 2015-04-11      3       3
cookie2 2015-04-10      2       3

 ②ROW_NUMBER:Row_number函數返回一個惟一的值,當碰到相同數據時,排名按照記錄集中記錄的順序依次遞增。 cookie

ROW_NUMBER() –從1開始,按照順序,生成分組內記錄的序列
–好比,按照pv降序排列,生成分組內天天的pv名次
ROW_NUMBER() 的應用場景很是多,再好比,獲取分組內排序第一的記錄;獲取一個session中的第一條refer等。
 
SELECT 
cookieid,
createtime,
pv,
ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn 
FROM lxw1234;
 
cookieid day           pv       rn
------------------------------------------- 
cookie1 2015-04-12      7       1
cookie1 2015-04-11      5       2
cookie1 2015-04-15      4       3
cookie1 2015-04-16      4       4
cookie1 2015-04-13      3       5
cookie1 2015-04-14      2       6
cookie1 2015-04-10      1       7
cookie2 2015-04-15      9       1
cookie2 2015-04-16      7       2
cookie2 2015-04-13      6       3
cookie2 2015-04-12      5       4
cookie2 2015-04-14      3       5
cookie2 2015-04-11      3       6
cookie2 2015-04-10      2       7

③RANK 和 DENSE_RANK:
RANK:Rank函數返回一個惟一的值,除非遇到相同的數據時,此時全部相同數據的排名是同樣的,同時會在最後一條相同記錄和下一條不一樣記錄的排名之間空出排名。
DENSE_RANK:Dense_rank函數返回一個惟一的值,除非當碰到相同數據時,此時全部相同數據的排名都是同樣的。 session

—RANK() 生成數據項在分組中的排名,排名相等會在名次中留下空位
—DENSE_RANK() 生成數據項在分組中的排名,排名相等會在名次中不會留下空位
 
SELECT 
cookieid,
createtime,
pv,
RANK() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn1,
DENSE_RANK() OVER(PARTITION BY cookieid ORDER BY pv desc) AS rn2,
ROW_NUMBER() OVER(PARTITION BY cookieid ORDER BY pv DESC) AS rn3 
FROM lxw1234 
WHERE cookieid = 'cookie1';
 
cookieid day           pv       rn1     rn2     rn3 
-------------------------------------------------- 
cookie1 2015-04-12      7       1       1       1
cookie1 2015-04-11      5       2       2       2
cookie1 2015-04-15      4       3       3       3
cookie1 2015-04-16      4       3       3       4
cookie1 2015-04-13      3       5       4       5
cookie1 2015-04-14      2       6       5       6
cookie1 2015-04-10      1       7       6       7
 
rn1: 15號和16號並列第3, 13號排第5
rn2: 15號和16號並列第3, 13號排第4
rn3: 若是相等,則按記錄值排序,生成惟一的次序,若是全部記錄值都相等,或許會隨機排吧。
相關文章
相關標籤/搜索