Hive日期處理

2021-10-08 16:41:36 字數 2940 閱讀 2508

1、求自然日、上一自然日、下一自然日、本周初/末、本月初/末、本季初/末、本年初/末、一月/三月/六月/一年前等

select 

calendar_day as calendar_day,                  --'自然日(日期型)'

regexp_replace(calendar_day,'-','') as calendar_dt,                   --'自然日(數值型)'

date_add(calendar_day, -1) as last_calendar_dt,              --'上一自然日'

date_add(calendar_day, 1) as next_calendar_dt,              --'下一自然日'

date_add(calendar_day,7-cast(default.date_format(calendar_day, 'u') as int)) as weekend_dt,                    --'本週末日期'

date_sub(default.add_months(calendar_day, 1), day(calendar_day)) as monthend_dt,                   --'本月末日期'

date_sub(default.add_months(to_date(concat(year(calendar_day),'-01-01')), 3*cast(ceil(month(calendar_day)/3) as int)), 1) as quartend_dt,                   --'本季末日期'

to_date(concat(year(calendar_day),'-12-31')) as yearend_dt,                    --'本年末日期'

date_add(calendar_day,1-cast(default.date_format(calendar_day,'u') as int)) as week_1st_dt,                   --'本周初日期'

date_sub(calendar_day,dayofmonth(calendar_day)-1) as month_1st_dt,                  --'本月初日期'

default.add_months(to_date(concat(year(calendar_day),'-01-01')), 3*cast(ceil(month(calendar_day)/3) - 1 as int)) as quart_1st_dt,                  --'本季初日期'

to_date(concat(year(calendar_day),'-01-01')) as year_1st_dt,                   --'本年初日期'

default.add_months(calendar_day,-1) as one_month_ago,                 --'乙個月前'

default.add_months(calendar_day,-3) as three_month_ago,               --'三個月前'

default.add_months(calendar_day,-6) as six_month_ago,                 --'六個月前'

default.add_months(calendar_day,-12) as one_year_ago,                  --'一年前'

default.add_months(calendar_day,-24) as two_year_ago,                  --'兩年前'

default.add_months(calendar_day,-36) as three_year_ago,                --'三年前'

default.add_months(calendar_day,-60) as five_year_ago,                 --'五年前'

default.add_months(calendar_day,-120) as ten_year_ago                  --'十年前'

from  (select date_add(date '2008-01-01', t1.pos) as calendar_day

from (select po***plode(split(space(datediff(date '2025-12-31', date '2008-01-01')),' '))) t1

)t2select t1.calendar_day, t1.mkt_code as mkt_code, if(isnotnull(t2.mkt_code), '是', '否') as if_trade_dt  from 

(select  

calendar_day,

mkt_code

from tmp_date t1

lateral view explode(split('001,003,004',',')) union_mkt_code as mkt_code) t1

left join (

select '001' as mkt_code, 

trade_days as calendar_dt  from  tmp_shanghai_date

union all

select '003' as mkt_code, 

trade_days as calendar_dt  from  tmp_hongkong_date

union all

select '004' as mkt_code,

trade_days as calendar_dt from  tmp_bank_date) t2

on t1.calendar_dt = t2.calendar_dt

and t1.mkt_code = t2.mkt_code

Hive的日期處理函式

1 date format函式 根據格式整理日期 hive gmall select date format 2019 02 10 yyyy mm 2019 02 2 date add函式 加減日期 hive gmall select date add 2019 02 10 1 2019 02 09...

Hive 時間日期處理總結

獲取當前時間截 select unix timestamp 結果 1539595903獲取當前時間1 select current timestamp 結果 2018 10 15 17 34 09.721獲取當前時間2 select from unixtime unix timestamp 結果 2...

Hive 時間日期處理總結

最近用hive比較多,雖然效率低,但是由於都是t 1的業務模式。所以也就不要求太多了,夠用就行。其中用的吧比較多就是時間了,由於大資料中很多欄位都不是標準的時間型別字段,所以期間涉及了很多的時間日期欄位的處理,這裡做乙個彙總。一遍以後查詢使用。也方便大家學習。1.先來乙個比較老的,select un...