hiveSQL常用句式整理(不定时更新)

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)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章