Hql 时间转换函数

2021-05-06  本文已影响0人  八爪鱼下水

函数总结:
select unix_timestamp('2019-07-01 23:59:00','yyyy-MM-dd HH:mm:ss');

转化时间戳
select from_unixtime(1561996740);

转回时间
substr(string A, int start, int len),substring(string A, int start, int len)

时间转换函数
substr(wce.create_time,12,2) as hourinfo, 转化小时
substr(wce.create_time,1,4) as yearinfo, 转换年
quarter(wce.create_time) as quarterinfo, 转化季度
substr(wce.create_time,6,2) as monthinfo, 转化月
substr(wce.create_time,9,2) as dayinfo 转化天

--输出 2020-01-10
--hive
select to_date(from_unixtime(UNIX_TIMESTAMP('20200110','yyyyMMdd')));

--presto
select (format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') ;

例子: 原时间为20200110 需先转化为标准日期形式再加减

--hive 
select date_add('2020-01-12',10);
select date_add(to_date(from_unixtime(UNIX_TIMESTAMP('20200110','yyyyMMdd'))),10);

--presto
select date_add('day',-6,cast('2020-07-07' as date)); 
--第三个参数不转换为date格式, 会报错 第三个参数必须为date格式
select 
date_add('day', -6, cast(format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') as date));
--hive
select from_unixtime(1578585600);
--加格式
select from_unixtime(1578585600,'yyyyMMdd');

--presto
select from_unixtime(1578585600);
--加格式
select format_datetime(from_unixtime(1578585600),'yyyy-MM-dd');
--hive 
select unix_timestamp('20200110' ,'yyyyMMdd'); --10位时间戳

-- presto 
select to_unixtime(cast('2020-01-10' as date));
select to_unixtime(cast(format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd') as date));
--hive
select datediff('2017-09-15','2017-09-01');-- 结果14

--presto 
select date_diff('day',cast('2018-09-05' as date),cast('2018-09-07' as date));

-- 1)需要提供参数'day',表示要查询的是天数间隔;要查询小时,则提供参数'hour'
-- 2)并且后面传参限制为date类型;
-- 3)最后要注意是后面减去前面 --与hive不同
--hive
select current_date;
select unix_timestamp(); --获取当前时间戳
select from_unixtime(unix_timestamp());

-- presto
select now();  --精确到今天的时分秒
select current_date; --精确到今天的年月日
select current_date - interval '1' day; 精确到昨天的年月日

--hive其它日期查询
select current_timestamp; --查询当前系统时间(包括毫秒数);  
select dayofmonth(current_date); -- 查询当月第几天
select last_day(current_date); --月末
select date_sub(current_date,dayofmonth(current_date)-1); --当月第1天: 
select add_months(date_sub(current_date,dayofmonth(current_date)-1),1); --下个月第1天
按id分组,把name连接为一行
select id,group_concat(name)
1|bobannahelen
2|tombabytom
按id分组,把name连接为一行,并按name升序
select id,group_concat(name order by name asc)
1|annabobhelen
2|babytomtom
按id分组,name去重并连接为一行,按name升序,用逗号分隔
select id,group_concat(distinct name order by name asc separator ‘,’)
1|anna,bob,helen
2|baby,tom

上一篇 下一篇

猜你喜欢

热点阅读