sqlplus中的sql时间维度表

ecbunoof  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(318)

我正在用oracle数据库(sqlplus)为大学做一个数据仓库项目。我需要创建时间维度表并填充它。table应该是这样的:

需要从2004年到2019年。
我尝试了不同的方法和查询,但都不起作用,遗憾的是,我对sqlplus的了解还不够,无法自己创建一个(或者成功地修改一个)。我完全迷路了。
非常感谢你的帮助和耐心。

osh3o9ms

osh3o9ms1#

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);
kxkpmulp

kxkpmulp2#

不要存储所有列;请改用虚拟列来计算派生数据,否则您会发现列可能不一致:

CREATE TABLE table_name (
  id             NUMBER(10,0)
                 GENERATED ALWAYS AS IDENTITY
                 CONSTRAINT table_name__id__pk PRIMARY KEY,
  "DATE"         DATE
                 CONSTRAINT table_name__date__nn NOT NULL
                 CONSTRAINT table_name__date__u UNIQUE
                 CONSTRAINT table_name__date__chk CHECK ( "DATE" = TRUNC( "DATE" ) ),
  id_day_of_week NUMBER(1,0) 
                 GENERATED ALWAYS AS ( "DATE" - TRUNC( "DATE", 'IW' ) + 1 ),
  day_of_week    VARCHAR2(9)
                 GENERATED ALWAYS AS ( CAST( TRIM( TO_CHAR( "DATE", 'DAY', 'NLS_DATE_LANGUAGE = AMERICAN' ) ) AS VARCHAR2(9) ) ),
  is_holiday     NUMBER(1,0)
                 CONSTRAINT table_name__id_holiday__chk CHECK ( is_holiday IN ( 0, 1 ) ),
  id_month       NUMBER(2,0)
                 GENERATED ALWAYS AS ( EXTRACT( MONTH FROM "DATE" ) ),
  month          VARCHAR2(9)
                 GENERATED ALWAYS AS ( CAST( TRIM( TO_CHAR( "DATE", 'MONTH', 'NLS_DATE_LANGUAGE = AMERICAN' ) ) AS VARCHAR2(9) ) ),
  id_year        NUMBER(5,0)
                 GENERATED ALWAYS AS ( EXTRACT( YEAR FROM "DATE" ) ),
  id_total       NUMBER(1,0)
                 GENERATED ALWAYS AS ( 1 ),
  total          CHAR(5)
                 GENERATED ALWAYS AS ( 'Total' )
);

注:
不应命名列 DATE 作为一个关键字,你需要用双引号将它括起来,并且每次使用它时都使用相同的大小写。
这个 id_day_of_week 是基于iso8601周的一天,因为依赖于 TO_CHAR( "DATE", 'D' ) 视情况而定 NLS_TERRITORY 设定一周中哪一天是第一天;这样,它与任何设置无关。
这个 day_of_week 以及 month 列有固定的语言。
不清楚是什么 id_total 以及 total 应包含,以便这些值作为文本值生成;如果要在这些列中包含非静态数据,请删除 GENERATED ... 宣言的一部分。
然后您可以使用以下方法填充它:

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

db<>在这里摆弄

相关问题