Oraclesql转Hivesql⼀些语法问题
⽬录
在⼀些数据仓库开发的业务场景,会经常遇到⼀些需要把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(条件,满⾜返回,不满⾜返回)