impala中常用日期函数

获取当前日期
select current_timestamp()和now()等价
得到这周是这年的第几周
weekofyear() 1-53周
eg:

#查找官网周活跃用户:
select weekofyear(opetime),count(distinct opeuserid) 
from  platform_kudu.visitlog 
where opeuserid is not null and page like '%.3vjia.com/%' and opetime<='2019-12-29' and opetime>='2019-11-29'
group by weekofyear(opetime)
#新注册用户周留存
select  weekofyear(regdate),count(distinct userid) from
 syscore_kudu.users t1
 where regdate>='2019-11-01 00:00:00' and 
 (select count(*)  from  3d_point_kudu.userinfo t2 
 where t1.userid=t2.userid and weekofyear(t1.regdate)+1=weekofyear(t2.sendtime) and year(t1.regdate)=year(t2.sendtime))>0
 group by weekofyear(regdate)

增加月份
add_months(timestamp date, int months)
add_months(timestamp date, bigint months)

select add_months(substr(opetime,1,10),1) from paltform_kudu.visitlog

增加日期
adddate(timestamp startdate, int days)
addmonth(timestamp startdate, bigint days)
eg. adddate(now(),1)

两个时间戳/字符串之间的时间差
unix_timestamp(string datetime, string format) 3获取的是数字

select userid,min(sendtime) min_time,max(sendtime) max_time,(unix_timestamp(max(sendtime))-unix_timestamp(min(sendtime)))/3600/24
from 3d_point_kudu.userinfo
where substr(sendtime,1,4) >= '2019'
and snedtime <= '2020-06-21 23:59:59'
group by userid

格式化日期
from_unixtime(bigint unixtime[, string format])
如:获取当前日期的前两天
一种做法:from_unixtime(unix_timestamp()-606024*2,‘yyyy-MM-dd’),当前日期时间戳减去两天的秒数;
另一种做法:substr(regexp_replace(cast(date_sub(now(),2 as string),’-’),1,8)),这个是 时间格式 string的处理方法

date_sub():减天数

日期转化操作
转换成date

select to_date(now())
select to_date('2020-07-09 10:39:30')

字符串转换成timestamp格式

select cast('2019-07-09' as timestamp)

日期的加减
当前日期+7天

select day_add(now(),7)

当前日期减去一个月

select month_add(now(),-1)

unixtime转换成yyyymmdd格式

#当前日期转换成yyyy-mm-dd格式
select from_unixtime(unix_timestamp(),'yyyy-mm-dd')

日期转字符串
当前日期加7天,并转换成yyyy-mm-dd格式

select from_unixtime(unix_timestamp(days_add(now(),7)),'yyyy-mm-dd')

字符串转日期
将字符串日期转化成yyyy-mm-dd格式

select from_unixtime(unix_timestamp(days_add(cast('2020-07-09' as timestamp),7)),'yyyy-mm-dd')

得到小时
hour()
增加小时
hours_add()
减少小时
hours_sub()
得到月份
month()
得到年
year()

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值