How can i generate a Date table with fields like "Dayofweek", "weekofyear" etc ;and rows equals to date from 2010-01-01 till current_date like below:
Dayofweek Dayofmonth Dayofyear Weekofmonth Weekofyear Holiday
2010-01-01 6 1 1 1 1 Y
2010-01-02 7 2 2 1 1 N
2010-01-03 1 3 3 1 1 N
.....
2019-03-31 1 31 90 6 14 N
P.S. DayofWeek = day num of the week, Sun = 1, Sat = 7 Dayofmonth = day num of the month
Holiday is a flag to distinguish whether the records is a public holiday
So first step i need to do may be create records from 2010-01-01 till current_date, i wonder while loop in hive and mssql will do? Then I have the column ready Finally combine them.
I have tried
"Declare @startdate date
Declare @enddate date
set @startdate = '2010-01-01'
set @end_date = current_date
while @ start_date <=end_date
BEGIN
DATEADD(DAY,1,@startdate)
END
"Declare @startdate date
Declare @enddate date
set @startdate = '2010-01-01'
set @end_date = current_date
while @ start_date <=end_date
BEGIN
DATEADD(DAY,1,@startdate)
END
Dayofweek Dayofmonth Dayofyear Weekofmonth Weekofyear Holiday
2010-01-01 6 1 1 1 1 Y
2010-01-02 7 2 2 1 1 N
2010-01-03 1 3 3 1 1 N
.....
2019-03-31 1 31 90 6 14 N
You can get a calendar table with the following solution:
The basic calendar table is based on this solution on StackOverflow. With the
INNER JOIN
you can add some holidays.With
DATEPART
you can get some information about the date.Solution for Hive:
Add more holidays.