获取当前日期
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
字符串转换成timestamp格式
日期的加减
当前日期+7天
当前日期减去一个月
unixtime转换成yyyymmdd格式
#当前日期转换成yyyy-mm-dd格式
select from_unixtime(unix_timestamp(),'yyyy-mm-dd')
日期转字符串
当前日期加7天,并转换成yyyy-mm-dd格式
字符串转日期
将字符串日期转化成yyyy-mm-dd格式
得到小时
hour()
增加小时
hours_add()
减少小时
hours_sub()
得到月份
month()
得到年
year()