hive时间函数的使用

时间函数的变形

select
     day                                                                                                   -- 时间
    ,date_add(day,1 - dayofweek(day))                                                  as week_first_day   -- 本周第一天_周日
    ,date_add(day,7 - dayofweek(day))                                                  as week_last_day    -- 本周最后一天_周六
    ,date_add(day,1 - case when dayofweek(day) = 1 then 7 else dayofweek(day) - 1 end) as week_first_day   -- 本周第一天_周一
    ,date_add(next_day(day,'MO'),-7)                                                   as week_first_day   -- 本周第一天_周一;
    ,date_add(day,7 - case when dayofweek(day) = 1 then 7 else dayofweek(day) - 1 end) as week_last_day    -- 本周最后一天_周日
    ,next_day(day,'TU')                                                                as next_tuesday     -- 当前日期的下个周二
    ,trunc(day,'MM')                                                                   as month_first_day  -- 当月第一天
    ,date_sub(current_date,dayofmonth(current_date)-1)                                 as month_first_day1 -- 当月第一天
    ,add_months(date_sub(current_date,dayofmonth(current_date)-1),1)                   as next_month_first_day -- 下月第一天
    ,last_day(day)                                                                     as month_last_day   -- 当月最后一天
    ,to_date(concat(year(day),'-',lpad(ceil(month(day)/3) * 3 -2,2,0),'-01'))          as season_first_day -- 当季第一天
    ,last_day(to_date(concat(year(day),'-',lpad(ceil(month(day)/3) * 3,2,0),'-01')))   as season_last_day  -- 当季最后一天
    ,trunc(day,'YY')                                                                   as year_first_day   -- 当年第一天
    ,last_day(add_months(trunc(day,'YY'),12))                                          as year_last_day    -- 当年最后一天
    ,weekofyear(day)                                                                   as weekofyear       -- 当年第几周
    ,second(day)                                                                       as second           -- 秒钟
    ,minute(day)                                                                       as minute           -- 分钟
    ,hour(day)                                                                         as hour             -- 小时
    ,day(day)                                                                          as day              -- 日期
    ,month(day)                                                                        as month            -- 月份
    ,lpad(ceil(month(day)/3),2,0)                                                      as season           -- 季度
    ,year(day)                                                                         as year             -- 年份
from (
    select '2021-01-12 01:01:01' as day union all
    select '2021-02-12 02:03:04' as day union all
    select '2021-03-12 03:05:07' as day union all
    select '2021-04-12 04:07:10' as day union all
    select '2021-05-12 05:09:13' as day union all
    select '2021-06-12 06:11:16' as day union all
    select '2021-07-12 07:13:19' as day union all
    select '2021-08-12 08:15:22' as day union all
    select '2021-09-12 09:17:25' as day union all
    select '2021-10-12 10:19:28' as day union all
    select '2021-11-12 11:21:31' as day union all
    select '2021-12-12 12:23:34' as day
) t1
;

1、获取当前时间戳

select unix_timestamp() ;

2.1、获取当前时间1(时分秒)

select current_timestamp()

2.2、获取当前时间2

SELECT from_unixtime(unix_timestamp());

3、获取当前时间(年月日)

select current_date()

4、日期差值:datediff(结束日期,开始日期),返回结束日期减去开始日期的天数。

select datediff(CURRENT_DATE,'2021-01-01') as datediff;

5、日期加减:date_add(时间,增加天数),返回值为时间天+增加天的日期;date_sub(时间,减少天数),返回日期减少天后的日期。

select date_add(current_date,365) as dateadd;

6、时间差:两个日期之间的小时差

select (hour('2021-12-27 10:00:00')-hour('2021-12-25 12:00:00')+(datediff('2021-12-27 10:00:00','2021-12-25 12:00:00'))*24) as hour_subValue;

或者先将年月日时分秒的格式转为 时间戳格式后再进行加减

分钟数:(UNIX_TIMESTAMP(service_end_time) - UNIX_TIMESTAMP(service_start_time))/60) 
小时数: ((UNIX_TIMESTAMP(service_end_time) - UNIX_TIMESTAMP(service_start_time))/60) ) /60

9、获取年、月、日、小时、分钟、秒、当年第几周

select
     year('2021-12-01 10:00:00')       as year
    ,month('2021-12-01 10:00:00')      as month
    ,day('2021-12-01 10:00:00')        as day
    ,hour('2021-12-01 10:00:00')       as hour
    ,minute('2021-12-01 10:00:00')     as minute
    ,second('2021-12-01 10:00:00')     as second
    ,weekofyear('2021-12-01 10:00:00') as weekofyear

10、转成日期

select to_date('2021-12-01 10:00:00') ; 

11、当月最后一天

   select  last_day('2021-12-01 10:03:01');

12、当月第一天

select trunc(current_date,'MM') as day;

13、当年第一天

select trunc(current_date,'YY') as day;

14、next_day,返回当前时间的下一个星期几所对应的日期

select next_day('2021-12-01 10:03:01', 'TU');

15、hive中怎么获取两个日期相减后的小时(精确到两位小数点),而且这两个日期有可能会出现一个日期有时分秒,一个日期没有时分秒的情况

-- hive中怎么获取两个日期相减后的小时(精确到两位小数点),而且这两个日期有可能会出现一个日期有时分秒,一个日期没有时分秒的情况
select
     t3.day1
    ,t3.day2
    ,t3.day  -- 日期
    ,t3.hour -- 小时
    ,t3.min  -- 分钟
    ,t3.day + t3.hour          as hour_diff_1
    ,t3.day + t3.hour + t3.min as hour_diff_2
    ,round((cast(cast(t3.day1 as timestamp) as bigint) - cast(cast(t3.day2 as timestamp) as bigint)) / 3600,2) as hour_diff_3 -- 最优
    ,(datediff(t3.day1,t3.day2) * 24) + (nvl(hour(t3.day1),0) - nvl(hour(t3.day2),0)) + round((nvl(minute(t3.day1),0) - nvl(minute(t3.day2),0)) / 60,2) as hour_diff_4
from (
    select
         t2.day1
        ,t2.day2
        ,(datediff(t2.day1,t2.day2) * 24)                  as day  -- 日期
        ,(hour(t2.day1) - hour(t2.day2))                   as hour -- 小时
        ,round((minute(t2.day1) - minute(t2.day2)) / 60,2) as min  -- 分钟
    from (
        select
             cast(t1.day1 as timestamp) as day1
            ,cast(t1.day2 as timestamp) as day2
        from (
            select '2018-01-03 02:30:00' as day1, '2018-01-02 23:00:00' as day2 union all
            select '2018-06-02 08:15:22' as day1, '2018-06-02 06:11:16' as day2 union all
            select '2018-07-04'          as day1, '2018-07-02 01:01:01' as day2
        ) t1
    ) t2
) t3
;
+------------------------+------------------------+------+-------+--------+--------------+--------------+--------------+--------------+--+
|          day1          |          day2          | day  | hour  |  min   | hour_diff_1  | hour_diff_2  | hour_diff_3  | hour_diff_4  |
+------------------------+------------------------+------+-------+--------+--------------+--------------+--------------+--------------+--+
| 2018-07-04 00:00:00.0  | 2018-07-02 01:01:01.0  | 48   | -1    | -0.02  | 47           | 46.98        | 46.98        | 46.98        |
| 2018-01-03 02:30:00.0  | 2018-01-02 23:00:00.0  | 24   | -21   | 0.5    | 3            | 3.5          | 3.5          | 3.5          |
| 2018-06-02 08:15:22.0  | 2018-06-02 06:11:16.0  | 0    | 2     | 0.07   | 2            | 2.07         | 2.07         | 2.07         |
+------------------------+------------------------+------+-------+--------+--------------+--------------+--------------+--------------+--+

你可能感兴趣的:(Hive,hive,hadoop,数据仓库)