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),'-','')

 

你可能感兴趣的:(SQL)