SYS_CALENDAR SQL Netezza 中的函数

SYS_CALENDAR function in SQL Netezza

我怎样才能 运行 在 SQL Netezza 中生成日历的查询,就像下面使用 Teradata 的查询一样

SELECT *
   FROM SYS_CALENDAR.Calendar
   where calendar_date BETWEEN '2021-01-01' and current_date
select ('2021-01-01'::date + (interval '1 day' * idx))::date  as the_date
from _v_vector_idx where the_date between '2021-01-01' and current_date;

Teradata 的 SYS_CALENDAR.Calendar1900-01-012100-12-31 之间的每个日期都有一行。最好重新创建一次,然后在 Netezza

中使用视图

一次性初始化

-- A one time table to house all dates between 1900 and 2100
create table t_calendar (dt date);

-- Insert all dates once. Alternatively this SQL can be used
-- in the view, but that will slow things down
INSERT INTO T_CALENDAR 
  SELECT '1900-01-01'::DATE + 
   (10000*a.idx + 1000*b.idx + 100*c.idx + 10*d.idx + e.idx) AS dt
  FROM   _v_vector_idx a,
         _v_vector_idx b,
         _v_vector_idx c,
         _v_vector_idx d,
         _v_vector_idx e
  WHERE  a.idx < 8 -- there are less than 80000 days between 1900-2100
       AND b.idx < 10
       AND c.idx < 10
       AND d.idx < 10
       AND e.idx < 10
       AND dt <= '2100-12-31' :: DATE  

完成后,日历视图可以定义如下

create or replace view calendar as
  select 
    dt as calendar_date, 
    date_part('dow', dt) as day_of_week,
    date_part('day', dt) as day_of_month,
    date_part('doy', dt) as day_of_year,
    date_part('days', '1900-01-01'::date - dt) as day_of_calendar
    -- add others as necessary
  from t_calendar

根据需要使用 view definition and Netezza date functions 向该视图添加更多列。