Hive常用日期函數

1、to_date:日期時間轉日期函數

select to_date('2020-04-10 10:00:00')
輸出:2020-04-10

2、from_unixtime:轉換unix時間戳到當前時區的時間格式

select from_unixtime(1578585840,’yyyyMMdd’);
輸出:20200410

3、unix_timestamp:獲取當前unix時間戳

select unix_timestamp()
輸出:1586488760
select unix_timestamp('2020-04-10 10:51:20')
輸出:1586487080

4、year、month、day、hour、minute、second、weekoryear:返回日期中的年、月、日、時、分、秒、當前週數

select year('2020-04-10 10:00:00')
輸出:2020
select month('2020-04-10 10:00:00')
輸出:04
select day('2020-04-10 10:00:00')
輸出:10
select hour('2020-04-10 10:00:00')
輸出:10
select minute('2020-04-10 10:09:21')
輸出:09
select second('2020-04-10 10:09:21')
輸出:21
select weekofyear('2020-04-10 10:09:21')
輸出:15

5、datediff:返回開始日期減去結束日期的天數

select datediff('2020-04-10','2020-04-01')
輸出:9

6、date_add、date_sub:返回日期後n天的日期、返回日期前n天的日期

select date_sub('2020-04-10',4);
輸出:2015-04-06
select date_add('2020-04-10',4);
輸出:2020-04-14

7、from_unixtime+unix_timestamp  Hive中yyyymmdd和yyyy-mm-dd格式之間轉換

思想:先轉換成時間戳,再由時間戳轉換爲對應格式
select from_unixtime(unix_timestamp('20200401','yyyymmdd'),'yyyy-mm-dd')
輸出:2020-04-01
select from_unixtime(unix_timestamp('2020-04-01','yyyy-mm-dd'),'yyyymmdd')
輸出:20200401
由pt=20200430得到月初和月末
select from_unixtime(unix_timestamp('20200430','yyyymmdd'),'yyyy-mm-dd')
輸出:2020-04-30
select trunc(from_unixtime(unix_timestamp('20200430','yyyymmdd'),'yyyy-mm-dd'),'MM')
輸出:2020-04-01

8、Hive中月、季度、周分組查詢

--月份
select substr(current_date,1,7) `月份`
--季度
select concat(year(confirm_date_time),'-Q',ceil(month(confirm_date_time)/3)) as `季度`
--周

9、Hive中兩個日期相差小時、相差分鐘

--相差小時
select (unix_timestamp('2020-04-25 12:03:55') - unix_timestamp('2020-04-25 11:03:55'))/3600
輸出:1
--相差分鐘
select (unix_timestamp('2020-04-25 12:03:55') - unix_timestamp('2020-04-25 11:03:55'))/60
輸出:60

10、計算某一個日期是星期幾:

SELECT if(pmod(datediff('2020-04-20', '1920-01-01') - 3, 7)='0', 7, pmod(datediff('2020-04-20', '1920-01-01') - 3, 7)) 
輸出:1

11、常用日期查詢

--計算兩時間  相差小時
select datediff(date_1,date_2)*24+(hour(date_1)-hour(date_2))
-- 指定日期找週一
concat(date_add('2017-12-01',case when pmod(datediff('2017-12-01','2012-01-01'),7)=0 
then -6 else - (pmod(datediff('2017-12-01','2012-01-01'),7)-1) end),' 00:00:00')
-- 指定日期找當月一號
select trunc('2020-04-10','MM')  -- trunc(current_date,'MM')
--當月月末
select date_sub(add_months(trunc(CURRENT_TIMESTAMP,'MM'),1),1)
--上月第一天
select add_months(trunc(current_date,'MM'),-1)
--上月月末
select date_sub(trunc(CURRENT_TIMESTAMP,'MM'),1)
--每月15號
select date_add(trunc(CURRENT_TIMESTAMP,'MM'),14)
--上月該天
select date_sub(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),1)
-- 下週一
SELECT next_day(date_sub(current_date,1),'MON')
-- 下週日
SELECT date_add(next_day(date_sub(current_date,1),'MON'),6)
-- 本週一
SELECT date_sub(next_day(date_sub(current_date,1),'MON'),7)
-- 本週日
SELECT date_sub(next_day(date_sub(current_date,1),'MON'),1)
-- 上週一
SELECT date_sub(next_day(date_sub(current_date,1),'MON'),14)
-- 上週日
SELECT date_sub(next_day(date_sub(current_date,1),'MON'),8)
--週日
SELECT date_add(date_flag, 7- if(pmod(datediff(date_flag, '1920-01-01') - 3, 7) = 0,
        7,pmod(datediff(date_flag, '1920-01-01') - 3, 7))     ) as  week_last_day   --並不常用

---字段拆分 變爲多行
select
		id
		,student_id
		,order_code --訂單號
		,split_eggshell_change_log_ids  --對應eggshell_change_log_XX表的id,用逗號分隔
	
	from stg.stg_learning__ycb_eggshell_order_da yeo
		lateral view explode(split(eggshell_change_log_ids, ',')) my_table as split_eggshell_change_log_ids
	where pt=regexp_replace(date_sub(current_date,1),'-','')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章