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

生成部门维度数据

Posted on 2015-08-14 17:17  徐正柱-  阅读(628)  评论(0编辑  收藏  举报

1.生成部门维度表

SELECT 
       FN_Get_SubtrDeptStrs(DEPT_KEY_PATH, DEPT_LEVELS) DEPT_KEY,
       FN_Get_SubtrDeptStrs(DEPT_NAME_PATH, DEPT_LEVELS) DEPT_NAME,
       DEPT_LEVELS DEPT_LEVEL,
       ISLEAF,
       PARENT_KEY,
       FN_Get_SubtrDeptStrs(DEPT_KEY_PATH, 1) DEPT_KEY1,
       FN_Get_SubtrDeptStrs(DEPT_NAME_PATH, 1) DEPT_NAME1,
       FN_Get_SubtrDeptStrs(DEPT_KEY_PATH, 2) DEPT_KEY2,
       FN_Get_SubtrDeptStrs(DEPT_NAME_PATH, 2) DEPT_NAME2,
       FN_Get_SubtrDeptStrs(DEPT_KEY_PATH, 3) DEPT_KEY3,
       FN_Get_SubtrDeptStrs(DEPT_NAME_PATH, 3) DEPT_NAME3,
       FN_Get_SubtrDeptStrs(DEPT_KEY_PATH, 4) DEPT_KEY4,
       FN_Get_SubtrDeptStrs(DEPT_NAME_PATH, 4) DEPT_NAME4
     
  FROM (SELECT SUBSTR(SYS_CONNECT_BY_PATH(DEPT_KEY, '^'), 1) || '^' DEPT_KEY_PATH,
               SUBSTR(SYS_CONNECT_BY_PATH(DEPT_NAME, '^'), 1) || '^' DEPT_NAME_PATH,             
               V.DEPT_KEY,
               V.DEPT_CODE,
               V.DEPT_NAME,
               V.PARENT_KEY,
               V.DEPT_LEVEL,
               LEVEL        DEPT_LEVELS,
               CONNECT_BY_ISLEAF ISLEAF
          FROM dw_hrm_subj.DIM_DW_DEPT_ALL_V V
         START WITH V.PARENT_KEY = 119
        CONNECT BY V.PARENT_KEY = PRIOR V.DEPT_KEY)
 ORDER BY DEPT_KEY_PATH;
生成维度数据

 

2.字符串截取函数

create or replace function FN_Get_SubtrDeptStrs(i_dept_strs in varchar2, i_str_seq in number) return varchar2 is
  v_str_start_posize number:=0;
  v_str_next_posize number:=0;
  v_str_length  number:=0;
  v_str              varchar2(2000):='';
begin
  
  v_str_start_posize:=instr(i_dept_strs,'^',1,i_str_seq);
  v_str_next_posize:=instr(i_dept_strs,'^',1,i_str_seq+1);
  v_str_length:=v_str_next_posize-v_str_start_posize;
  v_str:=substr(i_dept_strs,v_str_start_posize,v_str_length);
  v_str:=replace(v_str,'^','');
  

  return(v_str);
end FN_Get_SubtrDeptStrs;
部门字符串截取

 

3.生成时间维度

create or replace procedure P_CREATE_DIM_DATE_TIME is
V_END_DATE DATE:=ADD_MONTHS(SYSDATE, 36);
V_START_DATE DATE:=TO_DATE('1980-01-01 00:00:01','YYYY-MM-DD HH24:MI:SS');
I NUMBER:=1;

V_DATE_TIME_ID    NUMBER:=0;
V_DATE_TIME_NAME    VARCHAR2(120):='';
V_YEAR_ID    NUMBER:=0;
V_YEAR_NAME    VARCHAR2(10):='';
V_QUTER_ID    NUMBER:=0;
V_QUTER_NAME    VARCHAR2(30):='';
V_MONTH_ID    NUMBER:=0;
V_MONTH_NAME    VARCHAR2(30):='';
V_DAY_ID    NUMBER:=0;
V_DAY_NAME    VARCHAR2(30):='';
V_WEEK_NAME    VARCHAR2(30):='';
V_DATE_TIME_TYPE VARCHAR2(10):='';

V_END_DATE_TIME NUMBER:=0;
V_STA_DATE_TIME NUMBER:=0;
begin
     delete from   DIM_DATE_TIME ;
     commit;

  ----1.生成时间维度表的年度信息
      V_END_DATE_TIME :=TO_NUMBER(TO_CHAR(V_END_DATE,'YYYY'));
      V_STA_DATE_TIME :=TO_NUMBER(TO_CHAR(V_START_DATE,'YYYY'));
      WHILE V_STA_DATE_TIME<=V_END_DATE_TIME LOOP
      V_DATE_TIME_ID:=V_STA_DATE_TIME;
      V_DATE_TIME_NAME:=V_STA_DATE_TIME||'';
      V_YEAR_ID:=V_STA_DATE_TIME;
      V_YEAR_NAME:=V_STA_DATE_TIME||'';
      V_DATE_TIME_TYPE:='Y';
      INSERT INTO DIM_DATE_TIME
        (DATE_TIME_ID,
         DATE_TIME_NAME,
         YEAR_ID,
         YEAR_NAME,
         DATE_TIME_TYPE)
      VALUES
        (V_DATE_TIME_ID,
         V_DATE_TIME_NAME,
         V_YEAR_ID,
         V_YEAR_NAME,
         V_DATE_TIME_TYPE);
      V_STA_DATE_TIME:=V_STA_DATE_TIME+1;


      END LOOP;
      COMMIT;

   ----2.生成时间维度表的季度信息
      V_END_DATE_TIME :=TO_NUMBER(TO_CHAR(V_END_DATE,'YYYY'));
      V_STA_DATE_TIME :=TO_NUMBER(TO_CHAR(V_START_DATE,'YYYY'));
      WHILE V_STA_DATE_TIME<=V_END_DATE_TIME LOOP
            FOR I in 1..4 LOOP
                V_DATE_TIME_ID:=V_STA_DATE_TIME||I;
                V_DATE_TIME_NAME:=V_STA_DATE_TIME||''||I||'季度';
                V_YEAR_ID:=V_STA_DATE_TIME;
                V_YEAR_NAME:=V_STA_DATE_TIME||'';
                V_QUTER_ID:=V_STA_DATE_TIME||I;
                V_QUTER_NAME:=V_STA_DATE_TIME||''||I||'季度';
                V_DATE_TIME_TYPE:='Q';

                INSERT INTO DIM_DATE_TIME
                  (DATE_TIME_ID,
                   DATE_TIME_NAME,
                   YEAR_ID,
                   YEAR_NAME,
                   QUTER_ID,
                   QUTER_NAME,
                   DATE_TIME_TYPE)
                VALUES
                  (V_DATE_TIME_ID,
                   V_DATE_TIME_NAME,
                   V_YEAR_ID,
                   V_YEAR_NAME,
                   V_QUTER_ID,
                   V_QUTER_NAME,
                   V_DATE_TIME_TYPE);
            END LOOP;
             V_STA_DATE_TIME:=V_STA_DATE_TIME+1;
      END LOOP;
      COMMIT;


      ----3.生成时间维度表的月份信息
      FOR CUR_DATE IN
      (  SELECT
          C.DATE_TIME_ID,C.YEAR_ID,C.YEAR_NAME,C.QUTER_ID,C.QUTER_NAME,
          DECODE(MOD(ROWNUM,12),0,12,MOD(ROWNUM,12)) MONTH_NUM
          FROM
          (
          SELECT B.*FROM
            (
              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'
              UNION ALL
              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'
              UNION ALL
              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'
            )B ORDER BY B.DATE_TIME_ID ASC
          ) C
      )
      LOOP
          if CUR_DATE.MONTH_NUM<10 THEN
          V_DATE_TIME_ID:=CUR_DATE.YEAR_ID||'0'||CUR_DATE.MONTH_NUM;
          ELSE
          V_DATE_TIME_ID:=CUR_DATE.YEAR_ID||CUR_DATE.MONTH_NUM;
          END IF;
          V_DATE_TIME_NAME:=CUR_DATE.YEAR_NAME||CUR_DATE.MONTH_NUM||'';
          V_YEAR_ID:=CUR_DATE.YEAR_ID;
          V_YEAR_NAME:=CUR_DATE.YEAR_NAME;
          V_QUTER_ID:=CUR_DATE.QUTER_ID;
          V_QUTER_NAME:=CUR_DATE.QUTER_NAME;
          V_MONTH_ID:=V_DATE_TIME_ID;
          V_MONTH_NAME:=V_DATE_TIME_NAME;
          V_DATE_TIME_TYPE:='M';

          INSERT INTO DIM_DATE_TIME
            (DATE_TIME_ID,
             DATE_TIME_NAME,
             YEAR_ID,
             YEAR_NAME,
             QUTER_ID,
             QUTER_NAME,
             MONTH_ID,
             MONTH_NAME,
             DATE_TIME_TYPE
             )
          VALUES
            (V_DATE_TIME_ID,
             V_DATE_TIME_NAME,
             V_YEAR_ID,
             V_YEAR_NAME,
             V_QUTER_ID,
             V_QUTER_NAME,
             V_MONTH_ID,
             V_MONTH_NAME,
             V_DATE_TIME_TYPE
             );

      END LOOP;
      COMMIT;


      ----1.生成时间维度表的天信息

      WHILE V_START_DATE<V_END_DATE LOOP

      V_YEAR_ID:=TO_CHAR(V_START_DATE,'YYYY');
      V_YEAR_NAME:=TO_CHAR(V_START_DATE,'YYYY')||'';
      V_QUTER_ID:=TO_CHAR(V_START_DATE,'YYYY')||TO_CHAR(V_START_DATE,'Q');
      V_QUTER_NAME    :=TO_CHAR(V_START_DATE,'YYYY')||''||TO_CHAR(V_START_DATE,'Q')||'季度';
      V_MONTH_ID :=TO_CHAR(V_START_DATE,'YYYYMM');
      V_MONTH_NAME:=TO_CHAR(V_START_DATE,'YYYY')||''||TO_CHAR(V_START_DATE,'MM')||'';
      V_DATE_TIME_ID:=TO_CHAR(V_START_DATE,'YYYYMMDD');
      V_DATE_TIME_NAME:=V_MONTH_NAME||TO_CHAR(V_START_DATE,'DD')||'';
      V_DATE_TIME_TYPE:='D';
      V_DAY_ID:=V_DATE_TIME_ID;
      V_DAY_NAME:=V_DATE_TIME_NAME;
      V_WEEK_NAME:=TO_CHAR(V_START_DATE,'DAY');

      INSERT INTO DIM_DATE_TIME
        (DATE_TIME_ID,
         DATE_TIME_NAME,
         YEAR_ID,
         YEAR_NAME,
         QUTER_ID,
         QUTER_NAME,
         MONTH_ID,
         MONTH_NAME,
         DAY_ID,
         DAY_NAME,
         WEEK_NAME,
         DATE_TIME_TYPE)
      VALUES
        (V_DATE_TIME_ID,
         V_DATE_TIME_NAME,
         V_YEAR_ID,
         V_YEAR_NAME,
         V_QUTER_ID,
         V_QUTER_NAME,
         V_MONTH_ID,
         V_MONTH_NAME,
         V_DAY_ID,
         V_DAY_NAME,
         V_WEEK_NAME,
         V_DATE_TIME_TYPE);


      COMMIT;
      V_START_DATE:=V_START_DATE+1;

      END LOOP;
      COMMIT;


end P_CREATE_DIM_DATE_TIME;
生成时间维度
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)
   );
时间维度表结构
CREATE OR REPLACE PROCEDURE P_CREATE_DIM_WEEK IS
  V_END_DATE DATE:=ADD_MONTHS(SYSDATE, 36);
  V_START_DATE DATE:=TO_DATE('1980-01-01 00:00:01','YYYY-MM-DD HH24:MI:SS');

  V_YEAR NUMBER:=0;
  V_WEEK NUMBER:=0;
  V_MONTH NUMBER:=0;
  V_DATE_ID NUMBER:=0;
  V_ROW_NUM NUMBER:=0;

  V_WEEK_IN_YEAR_ID    NUMBER:=0;
  V_WEEK_IN_YEAR    VARCHAR2(120):='';
  V_WEEK_START_DATE    DATE:=NULL;
  V_WEEK_END_DATE    DATE:=NULL;
  V_DATE_TIME_TYPE VARCHAR2(10):='W';

  V_END_DATE_TIME NUMBER:=0;
  V_STA_DATE_TIME NUMBER:=0;
BEGIN
     DELETE FROM  DIM_WEEK_IN_YEAR ;
     COMMIT;

  ----1.生成时间维度表的年度信息
      V_END_DATE_TIME :=TO_NUMBER(TO_CHAR(V_END_DATE,'YYYY'));
      V_STA_DATE_TIME :=TO_NUMBER(TO_CHAR(V_START_DATE,'YYYY'));
      
      WHILE V_START_DATE<=V_END_DATE LOOP
        
         V_MONTH:=TO_NUMBER(TO_CHAR(V_START_DATE,'MM'));
         V_WEEK:=TO_NUMBER(TO_CHAR(V_START_DATE,'IW')); 
        
      IF V_MONTH<=1 AND V_WEEK >=50 THEN
        
         V_YEAR:=TO_NUMBER(TO_CHAR(V_START_DATE,'YYYY'))-1;
      ELSE  
         V_YEAR:=TO_NUMBER(TO_CHAR(V_START_DATE,'YYYY'));
         
      END IF;
      V_DATE_ID:=TO_NUMBER(TO_CHAR(V_START_DATE,'YYYYMMDD'));    
      V_WEEK_IN_YEAR_ID:=TO_NUMBER(TO_CHAR(V_YEAR) || TO_CHAR(V_START_DATE,'IW'));
      V_WEEK_IN_YEAR:=TO_CHAR(V_YEAR)||'年第'||TO_CHAR(V_START_DATE,'IW')||'';
      V_WEEK_START_DATE:=TO_DATE(TO_CHAR(TRUNC(TO_DATE(TO_CHAR(V_START_DATE,'YYYY-MM-DD'),'YYYY-MM-DD'),'IW'),'YYYY-MM-DD'),'YYYY-MM-DD');
      V_WEEK_END_DATE:=TO_DATE(TO_CHAR(TRUNC(TO_DATE(TO_CHAR(V_START_DATE,'YYYY-MM-DD'),'YYYY-MM-DD'),'IW') + 6,'YYYY-MM-DD'),'YYYY-MM-DD');

        INSERT INTO DIM_WEEK_IN_YEAR
          (
           DATE_ID,
           WEEK_IN_YEAR_ID,
           WEEK_IN_YEAR,
           WEEK_START_DATE,
           WEEK_END_DATE,
           DATE_TIME_TYPE
          )
        VALUES
          (V_DATE_ID,
           V_WEEK_IN_YEAR_ID,
           V_WEEK_IN_YEAR,
           V_WEEK_START_DATE,
           V_WEEK_END_DATE,
           V_DATE_TIME_TYPE
          );
       
      
        V_ROW_NUM:=V_ROW_NUM+1;
       
      IF MOD(V_ROW_NUM,1000)=0 THEN
       COMMIT;
      END IF;
      
       V_START_DATE:=V_START_DATE+1;
       
      END LOOP;
      COMMIT;



END P_CREATE_DIM_WEEK;
时间自然周
CREATE TABLE DIM_WEEK_IN_YEAR 
   (  DATE_ID NUMBER, 
      WEEK_IN_YEAR_ID NUMBER, 
      WEEK_IN_YEAR VARCHAR2(30), 
      WEEK_START_DATE DATE, 
      WEEK_END_DATE DATE, 
      DATE_TIME_TYPE VARCHAR2(10)
   ) ;
   COMMENT ON TABLE CDMDATAMARKET.DIM_WEEK_IN_YEAR  IS '时间维度年周';
时间自然周表结构