Как я могу сгенерировать таблицу Date с полями, равными "Dayofweek", "weekofyear" и т. Д., И строк, равных дате от 2010-01-01 до current_date - PullRequest
0 голосов
/ 31 марта 2019

Как я могу сгенерировать таблицу Date с полями, такими как "Dayofweek", "weekofyear" и т. Д., И строк, равных дате от 2010-01-01 до current_date, как показано ниже:

             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

PS DayofWeek = номер дня недели, вс = 1, сб = 7 Dayofmonth = номер дня месяца

 Holiday is a flag to distinguish whether the records is a public holiday

Поэтому первым шагом, который мне нужно сделать, может быть создание записей за 2010-01 гг.-01 до current_date, интересно, пока цикл в улье и mssql подойдут?Тогда у меня есть готовая колонка. Наконец, объедините их.

Я попробовал

"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

Ответы [ 2 ]

1 голос
/ 31 марта 2019

Решение для Hive:

set hivevar:start_date=2010-01-01; --replace with your start_date
set hivevar:end_date=current_date; --replace with your end_date

with date_range as 
(--this query generates date range
select date_add ('${hivevar:start_date}',s.i) as dt 
  from ( select posexplode(split(space(datediff(${hivevar:end_date},'${hivevar:start_date}')),' ')) as (i,x) ) s
),

holiday as (
select stack(5, --add more
             '01-01', 'New Year',
             '01-21', 'Martin Luther King Day',
             '02-18', 'Presidents Day',
             '05-27', 'Memorial Day',
             '07-04', 'Independence Day'
        ) as ( mtdt,holiday_name)
) 

select d.dt                                 as date,
       date_format(current_date,'u')        as dayofweek,
       day(dt)                              as dayofmonth,
       date_format(current_date,'D')        as dayofyear,
       date_format(current_date,'W')        as weekofmonth,
       weekofyear(dt)                       as weekofyear, 
       case when h.mtdt is not null then 'Y' else 'N' end as Holiday,
       h.holiday_name
  from date_range d 
       left join holiday h on substr(d.dt,6)= h.mtdt
;

Добавить больше праздников.

0 голосов
/ 31 марта 2019

Вы можете получить таблицу календаря со следующим решением:

SELECT calendar.*, 
    DATEPART(dw, calendar.date_value) AS DayOfWeek, 
    DATEPART(dd, calendar.date_value) AS DayOfMonth, 
    DATEPART(dy, calendar.date_value) AS DayOfYear,
    DATEPART(wk, calendar.date_value) AS WeekOfYear, 
    CASE WHEN holidays.date_value IS NULL THEN 'N' ELSE 'J' END AS Holiday 
FROM (
  SELECT DATEADD(DAY, t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0, '1970-01-01') AS date_value FROM
    (SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
    (SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
    (SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
    (SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
    (SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) calendar INNER JOIN (
  SELECT '2010-01-01' AS date_value UNION SELECT '2010-01-06'
) holidays ON calendar.date_value = holidays.date_value
WHERE calendar.date_value BETWEEN CAST('2010-01-01' AS DATE) AND GETDATE()
ORDER BY calendar.date_value

Базовая таблица календаря основана на этом решении на StackOverflow . С INNER JOIN вы можете добавить некоторые праздники.

С помощью DATEPART вы можете получить некоторую информацию о дате.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...