1.IF 條件語句的用法
if(bool , expr1, expr2)
如果bool爲True,則取expr1,否則取expr2
2.①字符串替換函數。
regexp_replace(orig_str, ‘pattern’, ‘replacement’)
第一個參數爲原始字符串orig_str. 第二個參數爲要搜索的模式,第三個參數爲要換爲的字符串。
注意 特殊字符如‘|’需要用轉義字符 ‘\\|’
②字符串截取函數
substr(string A, int start, int len)
3.列轉行數據
concat_ws(’,’, collect_list(col))
第一個參數爲連接的符號,第二個參數爲需要列轉行的列名。
4.聯結操作
left join(左聯接) 返回包括左表中的所有記錄和右表中聯結字段相等的記錄
right join(右聯接) 返回包括右表中的所有記錄和左表中聯結字段相等的記錄
inner join(等值連接) 只返回兩個表中聯結字段相等的行
5.空值字段賦值 nvl
nvl(string,replace_col),如果字段string爲null,則返回replace_col的值
6.分組求topN
row_number()函數基於over對象分組、排序的記過,爲每一行分組記錄返回一個序號,該序號從1開始,依次遞增,遇到新組則重新從1開始。
Select *
from table
row_number() over(partition by item order by score desc) rank
where rank<=2
7.行轉列
Select
name,
sum(case when item=數學 then score end) as math,
sum(case when item=英語 then score end) as english,
From table
Group by name
8.時間戳轉換爲日期/ 日期轉換爲時間戳
from_unixtime(bigint unixtime,string format)
unix_timestamp(string date)