zoukankan      html  css  js  c++  java
  • Hive sql常用函数公式整理

    1.变量中文重命名:

    hive sql 和SAS用中文进行变量重命名时要用反向引号(键盘1左边那个键位)

    2.日期格式转化

    year(H.l_date)*100 + month(H.l_date) as yue; 202001
    date_format(active_date,'%Y-%m')='2020-01'
    DATE_FORMAT(ACTIVE_DATE,"yMM")='202001'
    其他日期格式:yyyymmdd, 年月日;
        yyyymm,年月;
        mm,月
        dd,日
        yyyy-mm-dd
        yyyy-mm
        yyyymmddhh24miss,年月日时分秒
        yyyy-mm-dd hh24:mi:ss
        hh24miss
        yyyymmddhh24missff3,年月日时分秒毫秒

    3.间隔天数:
    datediff(2019-09-01,2019-08-01) 返回31
    4.返回日期格式:
    to_date(2019-09-01 18:30:00) 返回2019-09-01
    5.返回月初:
    trunc(current_date,'MM') 返回本月1号
    返回上月末:
    last_day(add_months(current_date(),-1))
    6.返回n天前:
    date_sub(2019-09-01 18:30:00,31) 返回2019-08-01

    date_sub('2016-08-01',1) 表示 2016-07-31

    date_add('2016-08-01',-1) 输出:2016-07-31

    7.返回n个月前:
    add_months(current_date(),-1) 前移一个月
    8.上月放款:
    active_date>=trunc(add_months(current_date(),-1),"MM") and active_date<=last_day(add_months(current_date(),-1))

    9.${p}
    sql查询命令运行后提供选择
    10.字段拼接:
    concat("a",m.loan_init_term) as `期数`
    concat('2018','08') 返回201808
    concat_ws(string SEP, string A, string B...)返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符
    hive> select concat_ws(',','ab','cd','e') from test1;
    返回:ab,cd,e
    11.nvl(x,y) Returns y if x is null else return x
    12.mod(n1,n2) 
    返回n1 除以n2 的余数。返回值的正负和n1相关。
    13.pmod(n1,n2) 
    返回n1 除以n2 的余数绝对值
    14. 切片
    substr(string A, int start, int len)或者substring(string A, int start, int len) 
    hive> select substr(‘string’,3,3) from test; 
    rin
    hive>select substring(‘string’,3,3) from test; 
    ing

    15.公式替换:
    regexp_replace(string A, string B, string C) 
    返回值: string,说明:将字符串A中的符合Java正则表达式B的部分替换为C。注意,在有些情况下要使用转义字符
    举例:hive> select regexp_replace('string','rin','ron') from ccs_acct
    返回strong

    16.去重(取最新时点记录)
    row_number() over(partition by acct_nbr order by create_time desc) as rn where rn=1
    17.等额本息计算(excel)
    每月还款额=-PMT(月利率,月数,放款总额,0) 
    18.等额本金计算
    每月还款额=月均本金+(放款金额-月均本金*i)*月利率
    19.通配符
    where city like '[ALN]%'----筛选city 以‘A’,‘L’,‘N’开头的记录
    where city like 'ne%'-----筛选city以'ne'开头的记录
    where city like '%ne%'-----筛选city包含'ne'的记录
    where city like '_enan'-----筛选city第一个字符之后是'enan'的记录
    20.其他日期相关汇总: 
    from_unixtime:转化unix时间戳到当前时区的时间格式 
    select from_unixtime(1587361271,'yyyyMMdd') from ccs_acct 
    输出:20200420 
    unix_timestamp:获取当前unix时间戳 
    select unix_timestamp(); 
    输出:1587361271 
    select unix_timestamp('2020-04-20 14:20:03') from ccs_acct
    输出:1587363603
    select unix_timestamp('2020-04-19 14:20:03') from ccs_acct 
    输出:1587277203

    hour:返回日期中的小时 
    select hour('2020-04-20 14:20:03'); 
    输出:14

    minute:返回日期中的分钟 
    select minute('2020-04-20 14:20:03'); 
    输出:20 

    second:返回日期中的秒 
    select second('2020-04-20 14:20:03'); 
    输出:3

    weekofyear:返回日期在当前周数 
    select weekofyear('2020-04-20 14:20:03'); 
    输出:17

  • 相关阅读:
    基于注解的mybatis(转)
    git分支删除
    java多线程同步(转)
    hadoop学习笔记(五):java api 操作hdfs
    java常用设计模式一:单例模式
    mysql CONCAT用法
    mysql date_sub用法
    hadoop学习笔记(四):hdfs常用命令
    try-catch+thows异常范围说明
    Python 类的多态
  • 原文地址:https://www.cnblogs.com/lvzw/p/12737630.html
Copyright © 2011-2022 走看看