datediff操作
select datediff("2020-01-10", "2020-01-01")
输出:
9
repeat操作
select repeat("o", 9);
输出:ooooooooo
split操作
select split("ooooooooo", "o");
输出:["","","","","","","","","",""]
posexplode操作
select posexplode(split("ooooooooo", "o"));
输出:
pos,val
0,""
1,""
2,""
3,""
4,""
5,""
6,""
7,""
8,""
9,""
得到连续的日期
with dates as(
select date_add("2020-01-01", a.pos) as d
from (select posexplode(split(repeat("o", datediff("2020-01-10", "2020-01-01")), "o"))) a
)
select * from dates;
输出:
2020-01-01
2020-01-02
2020-01-03
2020-01-04
2020-01-05
2020-01-06
2020-01-07
2020-01-08
2020-01-09
2020-01-10
with dates as (
select date_add("2020-01-01", a.pos) as d
from (
select posexplode(split(repeat("o", datediff("2020-01-10", "2020-01-01")), "o"))) a
)
select
d as d
,year(d) as year
,month(d) as month
,day(d) as day
,date_format(d, 'u') as daynumber_of_week
,date_format(d, 'EEEE') as dayname_of_week
,date_format(d, 'D') as daynumber_of_year
from dates
order by d;
小时维度表
with dates as (
select date_add("2020-01-01", a.pos) as d
from (select posexplode(split(repeat("o", datediff("2020-01-10", "2020-01-01")), "o"))) a
),
hours as (
select a.pos as h
from (select posexplode(split(repeat("o", 23), "o"))) a
)
select
from_unixtime(unix_timestamp(cast(d as timestamp)) + (h * 3600)) as dt
, d as d
, year(d) as year
, month(d) as month
, day(d) as day
, h as hour
, date_format(d, 'u') as daynumber_of_week
, date_format(d, 'EEEE') as dayname_of_week
, date_format(d, 'D') as daynumber_of_year
from dates
join hours
sort by dt
;