-1

please

how can I run a query that generate a calendar in SQL Netezza like the below query that works with Teradata

SELECT *
   FROM SYS_CALENDAR.Calendar
   where calendar_date BETWEEN '2021-01-01' and current_date

2 Answers2

1
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;
Mark F
  • 271
  • 1
  • 1
1

Teradata's SYS_CALENDAR.Calendar has a row for each date between 1900-01-01 and 2100-12-31. Its better to recreate that once and then use a view in Netezza

One time initialization

-- 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  

With this done, a calendar view can be defined as follows

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

Use the view definition and Netezza date functions to add more columns to this view as required.

Aniket Kulkarni
  • 471
  • 2
  • 2