从双重创建日期 table

Creating date table from dual

我一直在试图弄清楚如何从这段代码中创建一个 table,但无法弄清楚。非常感谢您的回答。

WITH 
  dat_param AS (SELECT SYSDATE AS dat FROM dual)

SELECT 
  LEVEL AS day_year
  ,TRUNC((SELECT dat FROM dat_param),'YYYY')+LEVEL-1 AS date_year
  ,TO_CHAR(TRUNC((SELECT dat FROM dat_param),'YYYY')+LEVEL-1,'YYYY-MM') AS part_year
  ,TO_NUMBER(TO_CHAR(TRUNC((SELECT dat FROM dat_param),'YYYY')+LEVEL-1,'D')) AS day_week

FROM   dual
CONNECT BY LEVEL <= ADD_MONTHS(TRUNC((SELECT dat FROM dat_param),'YEAR'),12)-TRUNC((SELECT dat FROM dat_param),'YEAR')

假设你使用的是Oracle,你可以使用CREATE TABLE AS:

CREATE TABLE t as
    WITH 
      dat_param AS (SELECT SYSDATE AS dat FROM dual)    
    SELECT 
      LEVEL AS day_year
      ,TRUNC((SELECT dat FROM dat_param),'YYYY')+LEVEL-1 AS date_year
      ,TO_CHAR(TRUNC((SELECT dat FROM dat_param),'YYYY')+LEVEL-1,'YYYY-MM') AS part_year
      ,TO_NUMBER(TO_CHAR(TRUNC((SELECT dat FROM dat_param),'YYYY')+LEVEL-1,'D')) AS day_week    
    FROM   dual
    CONNECT BY LEVEL <= ADD_MONTHS(TRUNC((SELECT dat FROM dat_param),'YEAR'),12)-TRUNC((SELECT dat FROM dat_param),'YEAR');

Here 是一个 db<>fiddle.