微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐