我需要使用以下内容填充 Oracle 11g 数据库中的日历表:calendar_date、week_number、week_year、day_since_2000、week_since_2000、month_since_2000、quarter_since_2000、week_of_month
该表已经填充了自 2000 年以来的数据,我现在需要使用 since_2000 列的负数填充到 1994 年的数据。我想除了 2000 年以来的一周和 2000 年以来的季度外,我什么都想好了。
EDIT1:刚刚注意到我自 200 年以来的一周和自 2000 年以来的一个月仍然一团糟。使用下面提到的答案后,自 2000 年以来的季度看起来不错。我正在用最新的查询更新。
EDIT2:由于缺少 trunc(),它无法正常工作。它现在工作正常。最新查询已更新。
这就是我用来做的:
/* Formatted on 2/1/2013 11:54:27 AM (QP5 v5.227.12220.39724) */
CREATE OR REPLACE PROCEDURE populate_d_calendar (start_date IN DATE,
end_date IN DATE)
AS
BEGIN
INSERT INTO d_calendar (calendar_date,
week_number,
week_year,
day_since_2000,
week_since_2000,
month_since_2000,
quarter_since_2000,
week_of_month)
SELECT dt, --Date
TO_CHAR (dt,
'ww'), --Week in the year
TO_CHAR (dt,
'yyyy'), --Year
TO_CHAR (dt - end_date), --Day Since 2000
TRUNC (TO_CHAR (dt - end_date) / 7), --Week Since 2000
TRUNC (MONTHS_BETWEEN (dt,
end_date)), --Month Since 2000
CASE
WHEN TO_CHAR (dt,
'MMDD') >= '0101'
AND TO_CHAR (dt,
'MMDD') < '0401'
THEN
0
+ 4
* TRUNC ( MONTHS_BETWEEN (TO_DATE ( '0101'
|| TO_CHAR (dt,
'yyyy'),
'mmddyyyy'),
TO_DATE ('01Jun2000'))
/ 12) --q1
WHEN TO_CHAR (dt,
'MMDD') >= '0401'
AND TO_CHAR (dt,
'MMDD') < '0701'
THEN
1
+ 4
* TRUNC ( MONTHS_BETWEEN (TO_DATE ( '0101'
|| TO_CHAR (dt,
'yyyy'),
'mmddyyyy'),
TO_DATE ('01Jun2000'))
/ 12) --q2
WHEN TO_CHAR (dt,
'MMDD') >= '0701'
AND TO_CHAR (dt,
'MMDD') < '1001'
THEN
2
+ 4
* TRUNC ( MONTHS_BETWEEN (TO_DATE ( '0101'
|| TO_CHAR (dt,
'yyyy'),
'mmddyyyy'),
TO_DATE ('01Jun2000'))
/ 12) --q3
WHEN TO_CHAR (dt,
'MMDD') >= '1001'
AND TO_CHAR (dt,
'MMDD') <= '1231'
THEN
3
+ 4
* TRUNC ( MONTHS_BETWEEN (TO_DATE ( '0101'
|| TO_CHAR (dt,
'yyyy'),
'mmddyyyy'),
TO_DATE ('01Jun2000'))
/ 12) --q4
END
quarters_since_2000, --Quarter Since 2000
TO_CHAR (dt,
'w') --Week of the month
FROM (SELECT start_date + LEVEL - 1 dt
FROM DUAL
CONNECT BY LEVEL <= end_date - start_date + 1);
END;
/