hivesql中常用的函式
阿新 • • 發佈:2021-01-30
hivesql中的常用函式
單行函式: 字串 : 切割: split("ab_c","_") 去空格:trim(" abc ") 拼接: concat("a","b") concat_ws(",","ab","cd")--> ab,cd 子串: substr("abcd",2) --> bcd substr("abcd",2,2) -->bc 轉大小寫: upper("abcd") lower("abcd") 型別轉換: cast("1" as int) cast("2019-06-18" as date) cast("1.08" as double) to_date("2019-06-18") 條件表達: select id,name,if(age>10 and age<15,"young","old"), case when age<10 then '兒童' when age between 10 and 20 then '少年' when age>=20 and age<40 then '青年' when age>=40 and age<50 then '中年' else '老年' end as flag case age when 10 then '10歲' when 20 then '20歲' else '其他' end as f2 數字運算 abs(-2385) floor(3.12) -> 3 ceiling(3.12) -> 4 round(3.123,2) -> 3.12 sqrt(4) -> 2 pow(4,0.5) -> 2 pow(4,2) -> 16 日期操作 year("2017-06-17") month("2017-06-17") day("2017-06-17") hour("2017-06-17 10:30:40") date_sub("2017-06-17",1) datediff("2017-05-31","2017-06-17") date_add("2017-06-17",1) unix_timestamp("2017/06/17 10:30:40","yyyy/MM/dd HH:mm:ss") from_unixtime(1497695440,"yyyy/MM/dd HH:mm:ss") 分組聚合函式: max min sum count avg collect_set("course") -- set會去重 collect_list("course") -- list不去重
name,course
zs, shuxue
zs, huaxue
zs, huaxue
zs, wuli
ls, yuwen
ls, yingyu
ls, dili
select name,collect_set("course")
from t
group by name
==》:
zs [shuxue,huaxue,wuli]
ls [yuwen,yingyu,dili]
select name,collect_list("course")
from t
group by name
==》:
zs [shuxue, huaxue,huaxue,wuli]
ls [yuwen,yingyu,dili]
視窗分析函式: row_number() over() 排序 打標籤 1 2 3 4 5 rank() over() 排名 遇到相同的值打同一個標籤 下一個值加對應的跳躍值 1 2 3 3 5 dense_rank() over() 排名 遇到相同的值打同一個標籤 下一個值不進行跳躍 1 2 3 3 4 ntile(n) over() 將資料切分成n個區,並返回屬於第幾個分割槽 sum() over() count() over() first_value() over() 取分組內排序後,截止到當前行,第一個值 last_value() over() 取分組內排序後,截止到當前行,最後一個值 lead() over() : LEAD(col,n,DEFAULT) 用於統計視窗內往下第n行值, 與LAG相反 lag() over() : LAG(col,n,DEFAULT) 用於統計視窗內往上第n行值