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(条件,满足返回,不满足返回)

posted @ 2021-08-17 18:57  落花桂  阅读(1111)  评论(0编辑  收藏  举报
返回顶端
Live2D