• Hive实战(4):Hive 函数(6)HiveSQL 高阶函数合集实战(四)条件判断、数值相关、时间相关


    条件判断

    if:使用频率 ★★★★★

    if(boolean testCondition, T valueTrue, T valueFalseOrNull):判断函数,很简单

    如果testCondition 为true就返回valueTrue,否则返回valueFalseOrNull

    1 --判断是否为user1用户
    2 select 
    3  distinct user_id,
    4  if(user_id='user1',true,false) as flag
    5 from wedw_tmp.tmp_url_info 

    case when :使用频率 ★★★★★

    CASE a WHEN b THEN c [WHEN d THEN e]  [ELSE f] END

    如果a=b就返回c,a=d就返回e,否则返回f  如CASE 4 WHEN 5  THEN 5 WHEN 4 THEN 4 ELSE 3 END 将返回4

    相比if,个人更倾向于使用case when

    1 --仍然以if上面的列子
    2 select 
    3  distinct user_id,
    4  case when user_id='user1' then 'true'
    5     when user_id='user2' then 'test'
    6  else 'false' end  as flag
    7 from wedw_tmp.tmp_url_info 

    coalesce:使用频率 ★★★★★

    COALESCE(T v1, T v2, …)

    返回第一非null的值,如果全部都为NULL就返回NULL

     1--该函数结合lead或者lag更容易贴近实际业务需求,这里使用lead,并取后3行的值作为当前行值
     2 select 
     3  user_id,
     4  visit_time,
     5  rank,
     6  lead_time,
     7  coalesce(visit_time,lead_time) as has_time
     8 from 
     9(
    10  select
    11  user_id,
    12  visit_time,
    13  visit_cnt,
    14  row_number() over(partition by user_id order by visit_date desc) as rank,
    15  lead(visit_time,3) over(partition by user_id order by visit_date desc) as lead_time
    16  from  wedw_tmp.tmp_url_info
    17  order by user_id
    18 )t

    数值相关

    round:使用频率 ★★

    round(DOUBLE a):返回对a四舍五入的BIGINT值,

    round(DOUBLE a, INT d):返回DOUBLE型d的保留n位小数的DOUBLW型的近似值

    该函数没什么可以讲解的

    1select round(4/3),round(4/3,2);
    2+------+-------+--+
    3| _c0  |  _c1  |
    4+------+-------+--+
    5| 1.0  | 1.33  |
    6+------+-------+--+

    ceil:使用频率 ★★★

    ceil(DOUBLE a), ceiling(DOUBLE a)

    求其不小于小给定实数的最小整数;向上取整

    1 select ceil(4/3),ceiling(4/3)

    floor:使用频率 ★★★

    floor(DOUBLE a):向下取整''

    1 select floor(4/3);

    hex:使用频率 ★

    hex(BIGINT a)/ hex(STRING a)/ hex(BINARY a)

    计算十六进制a的STRING类型,如果a为STRING类型就转换成字符相对应的十六进制

    该函数很少使用,主要是因为曾经遇到过关于emoj表情符脏数据,故使用该函数进行处理

    时间相关(比较简单)

    from_unxitime:使用频率 ★★★★★

    from_unixtime(bigint unixtime[, string format])

    将时间的秒值转换成format格式(format可为“yyyy-MM-dd hh:mm:ss”,“yyyy-MM-dd hh”,“yyyy-MM-dd hh:mm”等等)

    1select 
    2 unix_timestamp() as current_timestamp,--获取当前时间戳
    3 unix_timestamp('2020-09-01 12:03:22') as speical_timestamp,--指定时间对于的时间戳
    4 from_unixtime(unix_timestamp(),'yyyy-MM-dd')  as current_date --获取当前日期

    to_date:使用频率 ★★★★★

    to_date(string timestamp)

    返回时间字符串的日期部分

    1--最后得到2020-09-10
    2select to_date('2020-09-10 10:31:31') 

    year:使用频率 ★★★★★

    year(string date)

    返回时间字符串的年份部分

    1--最后得到2020
    2select year('2020-09-02')

    month:使用频率 ★★★★★

    month(string date)

    返回时间字符串的月份部分

    1 --最后得到09
    2 select month('2020-09-10')

    day:使用频率 ★★★★★

    day(string date)

    返回时间字符串的天

    1 --最后得到10
    2 select day('2002-09-10')

    date_add:使用频率 ★★★★★

    date_add(string startdate, int days)

    从开始时间startdate加上days

    1 --获取当前时间下未来一周的时间
    2 select date_add(now(),7) 
    3 --获取上周的时间
    4 select date_add(now(),-7)

    date_sub:使用频率 ★★★★★

    date_sub(string startdate, int days)

    从开始时间startdate减去days

    1 --获取当前时间下未来一周的时间
    2 select date_sub(now(),-7) 
    3 --获取上周的时间
    4 select date_sub(now(),7)
  • 相关阅读:
    Find all Windows Phone Application Bar Icon
    DELL PowerConnect 5548[初步]配置
    删除SD的分区
    Import/Export a database into Oracle
    索爱U100i排线更换笔记
    forrtl: severe (174): SIGSEGV, segmentation fault occurred
    下载功能和上传功能的实现
    自我小结:手动给GridView添加数据源
    删除指定目录内的文件
    GridView数据源中没有数据的时候显示表头
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/14380043.html
Copyright © 2020-2023  润新知