博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

创建时间维表并生成数据

Posted on 2013-10-21 16:31  徐正柱-  阅读(5151)  评论(0编辑  收藏  举报

一、创建时间维度表

create table DIM_DATE_TIME
(
  DATE_TIME_ID   NUMBER,
  DATE_TIME_NAME VARCHAR2(120),
  YEAR_ID        NUMBER,
  YEAR_NAME      VARCHAR2(10),
  QUTER_ID       NUMBER,
  QUTER_NAME     VARCHAR2(30),
  MONTH_ID       NUMBER,
  MONTH_NAME     VARCHAR2(30),
  DAY_ID         NUMBER,
  DAY_NAME       VARCHAR2(30),
  WEEK_NAME      VARCHAR2(30),
  DATE_TIME_TYPE VARCHAR2(10)
)
;
View Code

二、创建生成时间维度数据的存储过程

  1 create or replace procedure P_CREATE_DIM_DATE_TIME is
  2 V_END_DATE DATE:=ADD_MONTHS(SYSDATE, 36);
  3 V_START_DATE DATE:=TO_DATE('1980-01-01 00:00:01','YYYY-MM-DD HH24:MI:SS');
  4 I NUMBER:=1;
  5 
  6 V_DATE_TIME_ID    NUMBER:=0;
  7 V_DATE_TIME_NAME    VARCHAR2(120):='';
  8 V_YEAR_ID    NUMBER:=0;
  9 V_YEAR_NAME    VARCHAR2(10):='';
 10 V_QUTER_ID    NUMBER:=0;
 11 V_QUTER_NAME    VARCHAR2(30):='';
 12 V_MONTH_ID    NUMBER:=0;
 13 V_MONTH_NAME    VARCHAR2(30):='';
 14 V_DAY_ID    NUMBER:=0;
 15 V_DAY_NAME    VARCHAR2(30):='';
 16 V_WEEK_NAME    VARCHAR2(30):='';
 17 V_DATE_TIME_TYPE VARCHAR2(10):='';
 18 
 19 V_END_DATE_TIME NUMBER:=0;
 20 V_STA_DATE_TIME NUMBER:=0;
 21 begin
 22      delete from   DIM_DATE_TIME ;
 23      commit;
 24   
 25   ----1.生成时间维度表的年度信息
 26       V_END_DATE_TIME :=TO_NUMBER(TO_CHAR(V_END_DATE,'YYYY'));
 27       V_STA_DATE_TIME :=TO_NUMBER(TO_CHAR(V_START_DATE,'YYYY'));
 28       WHILE V_STA_DATE_TIME<=V_END_DATE_TIME LOOP
 29       V_DATE_TIME_ID:=V_STA_DATE_TIME;
 30       V_DATE_TIME_NAME:=V_STA_DATE_TIME||'';
 31       V_YEAR_ID:=V_STA_DATE_TIME;
 32       V_YEAR_NAME:=V_STA_DATE_TIME||'';
 33       V_DATE_TIME_TYPE:='Y';
 34       INSERT INTO DIM_DATE_TIME
 35         (DATE_TIME_ID,
 36          DATE_TIME_NAME,
 37          YEAR_ID,
 38          YEAR_NAME,
 39          DATE_TIME_TYPE)
 40       VALUES
 41         (V_DATE_TIME_ID,
 42          V_DATE_TIME_NAME,
 43          V_YEAR_ID,
 44          V_YEAR_NAME,
 45          V_DATE_TIME_TYPE);
 46       V_STA_DATE_TIME:=V_STA_DATE_TIME+1;
 47       
 48       
 49       END LOOP;
 50       COMMIT;
 51       
 52    ----2.生成时间维度表的季度信息
 53       V_END_DATE_TIME :=TO_NUMBER(TO_CHAR(V_END_DATE,'YYYY'));
 54       V_STA_DATE_TIME :=TO_NUMBER(TO_CHAR(V_START_DATE,'YYYY'));
 55       WHILE V_STA_DATE_TIME<=V_END_DATE_TIME LOOP
 56             FOR I in 1..4 LOOP
 57                 V_DATE_TIME_ID:=V_STA_DATE_TIME||I;
 58                 V_DATE_TIME_NAME:=V_STA_DATE_TIME||''||I||'季度';
 59                 V_YEAR_ID:=V_STA_DATE_TIME;
 60                 V_YEAR_NAME:=V_STA_DATE_TIME||'';
 61                 V_QUTER_ID:=V_STA_DATE_TIME||I;
 62                 V_QUTER_NAME:=V_STA_DATE_TIME||''||I||'季度';
 63                 V_DATE_TIME_TYPE:='Q';
 64                        
 65                 INSERT INTO DIM_DATE_TIME
 66                   (DATE_TIME_ID,
 67                    DATE_TIME_NAME,
 68                    YEAR_ID,
 69                    YEAR_NAME,
 70                    QUTER_ID,
 71                    QUTER_NAME,
 72                    DATE_TIME_TYPE)
 73                 VALUES
 74                   (V_DATE_TIME_ID,
 75                    V_DATE_TIME_NAME,
 76                    V_YEAR_ID,
 77                    V_YEAR_NAME,
 78                    V_QUTER_ID,
 79                    V_QUTER_NAME,
 80                    V_DATE_TIME_TYPE);
 81             END LOOP;
 82              V_STA_DATE_TIME:=V_STA_DATE_TIME+1;
 83       END LOOP;
 84       COMMIT;
 85       
 86       
 87       ----3.生成时间维度表的月份信息           
 88       FOR CUR_DATE IN 
 89       (  SELECT 
 90           C.DATE_TIME_ID,C.YEAR_ID,C.YEAR_NAME,C.QUTER_ID,C.QUTER_NAME,
 91           DECODE(MOD(ROWNUM,12),0,12,MOD(ROWNUM,12)) MONTH_NUM
 92           FROM
 93           (
 94           SELECT B.*FROM
 95             (
 96               SELECT A.DATE_TIME_ID,A.YEAR_ID,A.YEAR_NAME,A.QUTER_ID,A.QUTER_NAME FROM DIM_DATE_TIME A WHERE A.DATE_TIME_TYPE='Q'
 97               UNION ALL
 98               SELECT A.DATE_TIME_ID,A.YEAR_ID,A.YEAR_NAME,A.QUTER_ID,A.QUTER_NAME FROM DIM_DATE_TIME A WHERE A.DATE_TIME_TYPE='Q'
 99               UNION ALL
100               SELECT A.DATE_TIME_ID,A.YEAR_ID,A.YEAR_NAME,A.QUTER_ID,A.QUTER_NAME FROM DIM_DATE_TIME A WHERE A.DATE_TIME_TYPE='Q'
101             )B ORDER BY B.DATE_TIME_ID ASC
102           ) C
103       )
104       LOOP
105           if CUR_DATE.MONTH_NUM<10 THEN
106           V_DATE_TIME_ID:=CUR_DATE.YEAR_ID||'0'||CUR_DATE.MONTH_NUM;
107           ELSE
108           V_DATE_TIME_ID:=CUR_DATE.YEAR_ID||CUR_DATE.MONTH_NUM;
109           END IF;
110           V_DATE_TIME_NAME:=CUR_DATE.YEAR_NAME||CUR_DATE.MONTH_NUM||'';
111           V_YEAR_ID:=CUR_DATE.YEAR_ID;
112           V_YEAR_NAME:=CUR_DATE.YEAR_NAME;
113           V_QUTER_ID:=CUR_DATE.QUTER_ID;
114           V_QUTER_NAME:=CUR_DATE.QUTER_NAME;
115           V_MONTH_ID:=V_DATE_TIME_ID;
116           V_MONTH_NAME:=V_DATE_TIME_NAME;
117           V_DATE_TIME_TYPE:='M';
118           
119           INSERT INTO DIM_DATE_TIME
120             (DATE_TIME_ID,
121              DATE_TIME_NAME,
122              YEAR_ID,
123              YEAR_NAME,
124              QUTER_ID,
125              QUTER_NAME,
126              MONTH_ID,
127              MONTH_NAME,
128              DATE_TIME_TYPE
129              )
130           VALUES
131             (V_DATE_TIME_ID,
132              V_DATE_TIME_NAME,
133              V_YEAR_ID,
134              V_YEAR_NAME,
135              V_QUTER_ID,
136              V_QUTER_NAME,
137              V_MONTH_ID,
138              V_MONTH_NAME,
139              V_DATE_TIME_TYPE
140              );
141        
142       END LOOP;
143       COMMIT;
144       
145       
146       ----1.生成时间维度表的天信息
147       
148       WHILE V_START_DATE<V_END_DATE LOOP
149 
150       V_YEAR_ID:=TO_CHAR(V_START_DATE,'YYYY');
151       V_YEAR_NAME:=TO_CHAR(V_START_DATE,'YYYY')||'';
152       V_QUTER_ID:=TO_CHAR(V_START_DATE,'YYYY')||TO_CHAR(V_START_DATE,'Q');
153       V_QUTER_NAME    :=TO_CHAR(V_START_DATE,'YYYY')||''||TO_CHAR(V_START_DATE,'Q')||'季度';
154       V_MONTH_ID :=TO_CHAR(V_START_DATE,'YYYYMM');
155       V_MONTH_NAME:=TO_CHAR(V_START_DATE,'YYYY')||''||TO_CHAR(V_START_DATE,'MM')||'';
156       V_DATE_TIME_ID:=TO_CHAR(V_START_DATE,'YYYYMMDD');
157       V_DATE_TIME_NAME:=V_MONTH_NAME||TO_CHAR(V_START_DATE,'DD')||'';
158       V_DATE_TIME_TYPE:='D';
159       V_DAY_ID:=V_DATE_TIME_ID;
160       V_DAY_NAME:=V_DATE_TIME_NAME;
161       V_WEEK_NAME:=TO_CHAR(V_START_DATE,'DAY');
162 
163       INSERT INTO DIM_DATE_TIME
164         (DATE_TIME_ID,
165          DATE_TIME_NAME,
166          YEAR_ID,
167          YEAR_NAME,
168          QUTER_ID,
169          QUTER_NAME,
170          MONTH_ID,
171          MONTH_NAME,
172          DAY_ID,
173          DAY_NAME,
174          WEEK_NAME,
175          DATE_TIME_TYPE)
176       VALUES
177         (V_DATE_TIME_ID,
178          V_DATE_TIME_NAME,
179          V_YEAR_ID,
180          V_YEAR_NAME,
181          V_QUTER_ID,
182          V_QUTER_NAME,
183          V_MONTH_ID,
184          V_MONTH_NAME,
185          V_DAY_ID,
186          V_DAY_NAME,
187          V_WEEK_NAME,
188          V_DATE_TIME_TYPE);
189 
190          
191       COMMIT;
192       V_START_DATE:=V_START_DATE+1;
193       
194       END LOOP;
195       COMMIT;
196       
197       
198 end P_CREATE_DIM_DATE_TIME;
View Code