SQL Netezza 中的 SYS_CALENDAR 函数

问题描述

如何在 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 definitionNetezza date functions 向此视图添加更多列。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...