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-08
select 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:46
select date_format(current_date(),'yyyy-MM-dd');
输出:2021-08-14
select 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();
输出:1628906623
select 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.762
select 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');
输出:2021
select quarter('2021-08-14 10:05:20');
输出:3
select month('2021-08-14 10:05:20');
输出:8
select day('2021-08-14 10:05:20');
输出:14
9、hour/minute/second:返回日期中的时/分/秒

select hour('2021-08-14 10:05:20');
输出:10
select minute('2021-08-14 10:05:20');
输出:5
select 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:日期比较函数,返回开始日期减去结束日期的天数

hive 函数大全
zip

0星
超过10%的资源
568KB

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

select datediff('2021-08-14','2021-08-08');
输出:6
select datediff(current_date(),date_add(current_date(),-10));
输出:10
select 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-01
select 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,也可以是两个缩写字母可以是三个可以是全名。

hive函数大全.txt
txt

0星
超过10%的资源
28KB

下载
select next_day('2021-08-14', 'TU'); --得到021-08-14后的下个周二
输出:2021-08-17
select 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-14
select 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


原文链接:https://blog.csdn.net/weixin_42679482/article/details/119700107

实例:

店铺信息
dwd_business_info
字段名称 字段备注 定义
market_id 项目ID  
business_id 店铺ID  
area 面积  
type_id 业态ID  
floor_id 楼层ID  

 

 

 

dwd_member_consume_info 会员消费聚合表全量
字段名称 字段备注 定义
market_id 项目ID  
business_id 店铺ID  
mid 会员ID  
summary_time 付费时间  
payable_amount 订单总金额  
sale_amount 实付金额  
sale_total 购买订单 一天购买的订单数据
refund_amount 退款订单  
refund_total 退款金额  
create_time 创建时间  
update_time 修改时间  

 

 

with type as (
select market_id,business_id,type_id
from hst_dwd.dwd_business_info
)
select market_id,
type_id,
mid,
datediff(current_date(), max(summary_time)) as sale_period
from (
select market_id,
business_id,
mid,
summary_time
from hst_dwd.dwd_member_consume_info
where summary_time between add_months(date_add(FROM_UNIXTIME(UNIX_TIMESTAMP()), -1), -7) and date_add(current_date(),-1) ---当前时间-1天至其前8天得数据
) dd1 join type i5 on( dd1.market_id = i5.market_id and dd1.business_id = i5.business_id)
group by dd1.market_id,
type_id,mid

posted @ 2022-08-17 19:23  我是影子。  Views(301)  Comments(0Edit  收藏  举报