Hive 时间相关函数汇总

        使用hive进行时间处理的过程中,经常会用到一些时间函数,现整理一下hive中常用日期函数


1、取得当前日期时间:

--取得当前日期:select current_date();输出:2021-08-14--取得当前日期时间:select current_timestamp();输出:2021-08-14 13:14:57--hive取得当前时间戳:select unix_timestamp();输出:1628911641

2、to_date:日期时间转日期函数,返回日期时间字段中的日期部分,

说明:字符串必须为:yyyy-MM-dd格式。

select to_date('2021-08-14 13:34:12');输出:2021-08-14

3、from_unixtime:unix时间戳到转时间格式

说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

select from_unixtime(1323308945,’yyyy-MM-dd’);输出:2011-12-08select from_unixtime(1323308945,’yyyyMMdd’);输出:20111208--取得当前时间,相当于select current_timestamp();select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');输出:2021-08-14 03:14:57

4、date_format:日期、时间戳、字符串类型格式化输出标准时间格式

select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');输出:2021-08-14 11:14:46select date_format(current_date(),'yyyy-MM-dd');输出:2021-08-14select date_format('2021-08-14','yyyy-MM-dd HH:mm:ss'); 输出:2021-08-14 00:00:00

5、unix_timestamp:获取当前时间的unix时间戳和日期转UNIX时间戳函数

select unix_timestamp();输出:1628906623select unix_timestamp('2021-08-14 10:05:20');输出:1628935520

6、from_utc_timestamp/to_utc_timestamp:utc时间转换:

select from_utc_timestamp(current_timestamp(),8);输出:2021-08-14 11:10:27.762select to_utc_timestamp(current_timestamp(),8);输出:2021-08-14 11:10:56.085

7、to_unix_timestamp:日期转unix时间戳

select to_unix_timestamp('2021-08-14 11:10:27','yyyy-MM-dd HH:dd:ss');输出:1628593227

8、year/quarter/month/day:返回日期中的年/季度/月/日

select year('2021-08-14 10:05:20');输出:2021select quarter('2021-08-14 10:05:20');输出:3select month('2021-08-14 10:05:20');输出:8select day('2021-08-14 10:05:20');输出:14

9、hour/minute/second:返回日期中的时/分/秒

select hour('2021-08-14 10:05:20');输出:10select minute('2021-08-14 10:05:20');输出:5select second('2021-08-14 10:05:20');输出:20

10、weekofyear:返回日期在当年的第几周

select weekofyear('2021-08-14 10:05:20');输出:32

11、dayofweek:返回日期在当前周的第几天(注:周日为第1天)

select dayofweek('2021-08-14 10:05:20');  --周六输出:7

12、datediff:日期比较函数,返回开始日期减去结束日期的天数

说明:前者大于后者,返回值为正,否则,返回值为负。

select datediff('2021-08-14','2021-08-08');输出:6select datediff(current_date(),date_add(current_date(),-10));输出:10select datediff(current_date(),date_add(current_date(),10));输出:-10

13、date_sub:日期减少函数,返回日期前n天的日期

select date_sub('2021-08-14',6);输出:2021-08-08

14、date_add:日期增加函数,返回日期后n天的日期

select date_add('2021-08-14',6);输出:2021-08-20--取得昨天日期:select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);-- 取得明天日期:select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);

15、months_between:返回两个日期之间包含的月数(结果为double类型)

select months_between('2021-10-14','2021-05-04');输出:5.32258065

16、trunc:获取日期月初(参数MM),年初日期(参数YY)

select trunc(current_date(),'MM');输出:2021-08-01select trunc(current_date(),'YY');输出:2021-01-01

17、last_day:获取日期当月最后一天

select last_day(current_date());输出:2021-08-31

18、next_day:返回当前日期之后的下个星期几的日期

说明:参数为MO,TU,WE,TH,FR,SA,SU,也可以是两个缩写字母可以是三个可以是全名。

select next_day('2021-08-14', 'TU'); --得到021-08-14后的下个周二输出:2021-08-17select next_date('2020-01-01','Fri');  --得到2020-01-01后的下个周五输出:2020-01-03

19、from_unixtime+unix_timestamp:yyyymmdd和yyyy-mm-dd日期间切换

思路:先转换成时间戳,再由时间戳转换为对应格式。

select from_unixtime(unix_timestamp('20210814','yyyymmdd'),'yyyy-mm-dd');输出:2021-08-14select from_unixtime(unix_timestamp('2021-08-14','yyyy-mm-dd'),'yyyymmdd') ;输出:20210814

20、取最近30天数据

seelct * from table where datediff(current_timestamp(),create_time)<=30;--要从表中运行

21、两个日期相差多少小时

select (unix_timestamp('2021-08-14 10:18:54')-unix_timestamp('2021-08-14 08:18:54'))/3600;输出:2.0

22、两个日期相差多少分钟

select (unix_timestamp('2021-08-14 10:18:54')-unix_timestamp('2021-08-14 08:18:54'))/60输出:120.0

23、返回上个月第一天和最后一天

--上个月第一天select trunc(add_months(current_timestamp(),-1),'MM');select concat(substr(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),1,7),'-01');select trunc(add_months(current_timestamp(),-1),'MM');输出:2021-07-01--上个月最后一天select last_day(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1)) ;select date_sub(trunc(current_timestamp(),'MM'),1);输出:2021-07-31-- 获取当月第一天select trunc(current_timestamp(),'MM');select from_unixtime(unix_timestamp(),'yyyy-MM-01');输出:2021-08-01-- 获取当月最后一天select last_day(current_timestamp());select  last_day(current_date());输出:2021-08-31

  • 17
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive开窗函数是一种用于在查询结果中执行聚合计算的函数。它们可以在分组的数据上执行计算,并返回结果集中的每一行的聚合值。开窗函数可以使用PARTITION BY子句对数据进行分组,并使用ORDER BY子句对数据进行排序。然后,可以在开窗函数内部使用聚合函数(如SUM、AVG、MAX、MIN、COUNT)对每个分组的数据进行计算。\[2\] 下面是一个使用Hive开窗函数的示例: ``` SELECT user_id, create_date, LAG(create_date, 1, '0000-00-00') OVER (PARTITION BY user_id) AS lag_date, LEAD(create_date, 1, '9999-99-99') OVER (PARTITION BY user_id) AS lead_date, FIRST_VALUE(create_date) OVER (PARTITION BY user_id) AS first_date, LAST_VALUE(create_date) OVER (PARTITION BY user_id) AS last_date FROM order_info; ``` 在这个示例中,我们使用了LAG、LEAD、FIRST_VALUE和LAST_VALUE函数来计算每个用户的前一天日期、后一天日期、第一个日期和最后一个日期。这些函数在每个用户分组内进行计算,并返回结果集中的每一行的相应值。\[2\] 除了开窗函数Hive还提供了其他函数,如NVL函数和CASE WHEN THEN ELSE END函数。NVL函数用于处理空值,可以将空值替换为指定的默认值。CASE WHEN THEN ELSE END函数类似于C语言中的case语句,用于根据条件对字段值进行判断和处理。\[1\]\[3\] #### 引用[.reference_title] - *1* *3* [Hive——Hive常用内置函数总结](https://blog.csdn.net/weixin_44606952/article/details/127929532)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [HIVE函数汇总--开窗函数篇](https://blog.csdn.net/weixin_43935266/article/details/122307605)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值