create table date_dim
(id number(38),
date date,
id_dayofweek number(38),
dayofweek varchar(100),
id_holiday number(38),
id_month number(38),
month varchar(100),
id_year number(38),
id_total number(38),
Total varchar(100));
使用上面的查询创建表。 对于数据,可以通过connect by子句生成。
insert into date_dim
(select level as id, to_date('31-DEC-2003', 'DD-MON-YYYY') + level as date1,
case when ltrim(rtrim(to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Day'))) = 'Monday' then 2
when ltrim(rtrim(to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Day'))) = 'Tuesday' then 3
when ltrim(rtrim(to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Day'))) = 'Wednesday' then 4
when ltrim(rtrim(to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Day'))) = 'Thursday' then 5
when ltrim(rtrim(to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Day'))) = 'Friday' then 6
when ltrim(rtrim(to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Day'))) = 'Saturday' then 7
when ltrim(rtrim(to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Day'))) = 'Sunday' then 1 end as id_dayofweek,
to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Day') as dayofweek,
0 as id_holiday,
to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'MM') as id_month,
to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'Month') as month,
to_char(to_date('31-DEC-2003', 'DD-MON-YYYY') + level, 'YYYY') as year,
1 as id_total,
'Total' as Total
from dual
connect by level < = 5844);
INSERT INTO table_name ( "DATE", is_holiday )
SELECT DATE '2004-01-01' + LEVEL - 1, 0
FROM DUAL
CONNECT BY DATE '2004-01-01' + LEVEL - 1 < DATE '2020-01-01';
并使用 UPDATE 根据你的领土发表声明。 如果你这样做了:
SELECT *
FROM table_name
ORDER BY "DATE" ASC
FETCH FIRST 32 ROWS ONLY;
输出为:
ID | DATE | ID_DAY_OF_WEEK | DAY_OF_WEEK | IS_HOLIDAY | ID_MONTH | MONTH | ID_YEAR | ID_TOTAL | TOTAL
-: | :-------- | -------------: | :---------- | ---------: | -------: | :------- | ------: | -------: | :----
1 | 01-JAN-04 | 4 | THURSDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
2 | 02-JAN-04 | 5 | FRIDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
3 | 03-JAN-04 | 6 | SATURDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
4 | 04-JAN-04 | 7 | SUNDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
5 | 05-JAN-04 | 1 | MONDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
6 | 06-JAN-04 | 2 | TUESDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
7 | 07-JAN-04 | 3 | WEDNESDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
8 | 08-JAN-04 | 4 | THURSDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
9 | 09-JAN-04 | 5 | FRIDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
10 | 10-JAN-04 | 6 | SATURDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
11 | 11-JAN-04 | 7 | SUNDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
12 | 12-JAN-04 | 1 | MONDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
13 | 13-JAN-04 | 2 | TUESDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
14 | 14-JAN-04 | 3 | WEDNESDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
15 | 15-JAN-04 | 4 | THURSDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
16 | 16-JAN-04 | 5 | FRIDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
17 | 17-JAN-04 | 6 | SATURDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
18 | 18-JAN-04 | 7 | SUNDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
19 | 19-JAN-04 | 1 | MONDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
20 | 20-JAN-04 | 2 | TUESDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
21 | 21-JAN-04 | 3 | WEDNESDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
22 | 22-JAN-04 | 4 | THURSDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
23 | 23-JAN-04 | 5 | FRIDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
24 | 24-JAN-04 | 6 | SATURDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
25 | 25-JAN-04 | 7 | SUNDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
26 | 26-JAN-04 | 1 | MONDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
27 | 27-JAN-04 | 2 | TUESDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
28 | 28-JAN-04 | 3 | WEDNESDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
29 | 29-JAN-04 | 4 | THURSDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
30 | 30-JAN-04 | 5 | FRIDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
31 | 31-JAN-04 | 6 | SATURDAY | 0 | 1 | JANUARY | 2004 | 1 | Total
32 | 01-FEB-04 | 7 | SUNDAY | 0 | 2 | FEBRUARY | 2004 | 1 | Total
2条答案
按热度按时间osh3o9ms1#
使用上面的查询创建表。
对于数据,可以通过connect by子句生成。
kxkpmulp2#
不要存储所有列;请改用虚拟列来计算派生数据,否则您会发现列可能不一致:
注:
不应命名列
DATE
作为一个关键字,你需要用双引号将它括起来,并且每次使用它时都使用相同的大小写。这个
id_day_of_week
是基于iso8601周的一天,因为依赖于TO_CHAR( "DATE", 'D' )
视情况而定NLS_TERRITORY
设定一周中哪一天是第一天;这样,它与任何设置无关。这个
day_of_week
以及month
列有固定的语言。不清楚是什么
id_total
以及total
应包含,以便这些值作为文本值生成;如果要在这些列中包含非静态数据,请删除GENERATED ...
宣言的一部分。然后您可以使用以下方法填充它:
并使用
UPDATE
根据你的领土发表声明。如果你这样做了:
输出为:
db<>在这里摆弄