zoukankan      html  css  js  c++  java
  • 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(条件,满足返回,不满足返回)

    作者:落花桂
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    arm基础
    数据的封装
    网络安全基础
    qt5学习笔记
    nginx修改配置
    proteus_base1
    20191022
    20191015
    20191014
    20191013
  • 原文地址:https://www.cnblogs.com/nthforsth/p/15040937.html
Copyright © 2011-2022 走看看