Oracle sql 轉 Hive sql一些語法問題


  在一些數據倉庫開發的業務場景,會經常遇到一些需要把oracle的查詢語句轉成 hive的查詢語句
  推薦一篇博主的文章 ===> 【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')

8、Oracle的 decode()

oracle:

decode(條件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值)

hive:
hive中的decode的函數並不是這樣的功能,但可以用case when去做實現,或者用if(條件,滿足返回,不滿足返回)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM