1. 程式人生 > 其它 >Oracle sql 轉 Hive sql一些語法問題

Oracle sql 轉 Hive sql一些語法問題

目錄

  在一些資料倉庫開發的業務場景,會經常遇到一些需要把oracle的查詢語句轉成 hive的查詢語句

1、時間格式1

oracle:

to_chara(XXdate,'yyyyy-MM-dd hh24:mi:ss')
------------------------------------------
2021-07-16 17:23:51 => 2021-07-16 17:23:51

hive: (使用cast函式進行資料型別轉換)

cast(XXdate as string)
------------------------------------------
2021-07-16 17:23:51.0 => 2021-07-16 17:23:51

2、時間格式2

oracle:

trunc(XXdate)
------------------------------------------
2021-07-16 17:23:51.0 => 2021-07-16 00:00:00

hive:

date_format(XXdate,'yyyy-MM-dd')
------------------------------------------
2021-07-16 17:23:51.0 => 2021-07-16

3、字串拼接

oracle:

select col1 || ',' || col2 from table

hive:(使用字元拼接函式:concat_ws()、concat()等)

select concat_ws(',' ,col1,col2) from table

4、TopN問題

限制行數輸出

oracle:

select * from table where rownum<=10

hive:

select * from table limit 10

5、左外連線

oracle:

select * from T1,T2 where T1.id=T2.id(+)

hive:

select * from T1 left join T2 on T1.id=T2.id

6、獲取當月第一日

hive中也有trunc()函式,但格式化時有區別

oracle:

trunc(sysdate,'mm')

hive:

trunc(current_date,'MM')

7、獲取上個月的第一日

oracle:

add_months(trunc(sysdate, 'mm'),-1)

hive:

add_months(trunc(current_date,'MM'),-1)

8、時間格式3

oracle:

to_char(XXdate,'yyyymm')
2021-07-21 00:00:00 => 202107

hive:

date_format(XXdate,'yyyyMM')
作者:落花桂     出處:http://grenet.cnblogs.com/     本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。