1

我想加载从 1995 年 1 月 1 日到 2023 年 12 月 31 日的数据。正确的语法是什么。目前在以下查询的帮助下,我只有 3 年的数据(1096 条记录)。

CREATE TABLE "DATE_DIM" 
   (    "DATE_KEY" NUMBER(10,0) CONSTRAINT "NN_DATE_KEY" NOT NULL ENABLE, 
    "DATE_VALUE" DATE, 
    "DATE_DAY_OF_WEEK" NUMBER, 
    "DATE_DAY_OF_WEEK_NAME" VARCHAR2(10 BYTE), 
    "DATE_WEEKEND_FLAG" NUMBER, 
    "DATE_WEEK_IN_MONTH" NUMBER, 
    "DATE_WEEK_IN_YEAR" NUMBER, 
    "DATE_WEEK_START_DATE" DATE, 
    "DATE_WEEK_END_DATE" DATE, 
    "DATE_DAY_OF_MONTH" NUMBER, 
    "DATE_MONTH_START_DATE" DATE, 
    "DATE_MONTH_NUMBER" VARCHAR2(2 BYTE), 
    "DATE_MONTH_NAME" VARCHAR2(10 BYTE), 
    "DATE_MONTH_ABBR" VARCHAR2(3 BYTE), 
    "DATE_QUARTER_NUMBER" VARCHAR2(1 BYTE), 
    "DATE_QUARTER_NAME" VARCHAR2(2 BYTE), 
    "DATE_QUARTER" VARCHAR2(8 BYTE), 
    "DATE_FISCAL_YEAR" VARCHAR2(4 BYTE), 
    "DATE_DAY_OF_YEAR_NUMBER" NUMBER, 
    "DATE_DAYS_IN_YEAR" NUMBER, 
    "DATE_HOLIDAY_IND" VARCHAR2(1 BYTE), 
     CONSTRAINT "PK_DATE_DIM" PRIMARY KEY ("DATE_KEY");



INSERT INTO date_dim 
 SELECT TO_NUMBER (TO_CHAR (mydate, 'yyyymmdd')) AS date_key,
      TO_CHAR (mydate, 'dd-MON-yyyy') AS date_value,
      TO_NUMBER (TO_CHAR (mydate, 'D')) AS date_day_of_week,
      TO_CHAR (mydate, 'Day') AS date_day_of_week_name,
      CASE WHEN TO_NUMBER (TO_CHAR (mydate, 'D')) IN (1, 7) THEN 1
           ELSE 0
       END AS date_weekend_flag,
      TO_NUMBER (TO_CHAR (mydate, 'W')) AS date_week_in_month,
      TO_NUMBER (TO_CHAR (mydate, 'WW')) AS date_week_in_year,
      TRUNC(mydate, 'w') AS date_week_start_date,
      TRUNC(mydate, 'w') + 7 - 1/864 AS date_week_end_date,
      TO_NUMBER (TO_CHAR (mydate, 'DD')) AS date_day_of_month,
      to_date(to_char(mydate,'MM') || '01' || to_char(mydate,'YYYY'),'MMDDYYYY') AS date_Month_START_DATE,
      TO_CHAR (mydate, 'MM') AS date_month_number,
      TO_CHAR (mydate, 'Month') AS date_month_name,
      TO_CHAR (mydate, 'MON') AS date_month_abbr,
      TO_CHAR (mydate, 'Q') AS date_quarter_number,
      'Q' || TO_CHAR (mydate, 'Q') AS date_quarter_name,
       'Q'
    || UPPER(TO_CHAR(mydate,'Q')
    || '-'
    || TO_CHAR(mydate,'YYYY'))     AS date_quarter,
            TO_CHAR (mydate, 'yyyy') AS date_fiscal_year,
      TO_NUMBER (TO_CHAR (mydate, 'DDD')) AS date_day_of_year_number,
     ADD_MONTHS (TRUNC (mydate, 'Y'), 12) - TRUNC (mydate, 'Y') AS date_days_in_year,

   ' ' as DATE_HOLIDAY_FLAG
  FROM ( SELECT TRUNC (ADD_MONTHS (SYSDATE, -12), 'yy') - 1 + LEVEL AS mydate
          FROM dual
         CONNECT BY LEVEL <= (SELECT   TRUNC (ADD_MONTHS (SYSDATE, 24), 'yy')
                                     - TRUNC (ADD_MONTHS (SYSDATE, -12), 'yy')
                               FROM DUAL
                             )
       );
4

1 回答 1

2

我假设您确实是在询问带有connect by. 对于那些固定日期(仅涵盖 29 年,不是吗?),您可以使用:

  FROM ( SELECT DATE '1995-01-01' - 1 + LEVEL AS mydate
          FROM dual
         CONNECT BY LEVEL <= (DATE '2024-01-01' - DATE '1995-01-01')
       )

...生成 10592 条记录,涵盖 01-JAN-95 到 31-DEC-23。

我不确定你为什么要存储这么多重复的东西并且可以即时计算(例如一年中的几天),我会考虑将其设为视图而不是表格 - 特别是如果它变成你需要一个滚动窗口 - 但你需要考虑我认为的性能。

这很奇怪:

      TO_CHAR (mydate, 'dd-MON-yyyy') AS date_value,

您的表格中的DATE_VALUE列是一种DATE类型,因此您在插入过程中进行了隐式转换 - 您将 a 转换DATE为 aVARCHAR并返回 a DATE,无缘无故;你可以这样做:

      mydate AS date_value,

可能还有其他...而且值得指出的是DDAY格式取决于您的 NLS 设置。

于 2013-07-03T19:45:59.450 回答