首页 > 代码库 > 北京PK10数据分析项目

北京PK10数据分析项目

创建src表

CREATE TABLE "SRC     "."PK10"  (
                  "DATE_ID" DATE NOT NULL ,
                  "CODE_ID" INTEGER NOT NULL ,
                  "A" INTEGER ,
                  "B" INTEGER ,
                  "C" INTEGER ,
                  "D" INTEGER ,
                  "E" INTEGER ,
                  "F" INTEGER ,
                  "G" INTEGER ,
                  "H" INTEGER ,
                  "I" INTEGER ,
                  "J" INTEGER )
                 COMPRESS YES
                 DISTRIBUTE BY HASH("DATE_ID")
                   IN "USERSPACE1" ;

创建ODS表

CREATE TABLE "ODS     "."PK10"  (
                  "DATE_ID" DATE NOT NULL ,
                  "CODE_ID" INTEGER NOT NULL ,
                  "A" INTEGER ,
                  "B" INTEGER ,
                  "C" INTEGER ,
                  "D" INTEGER ,
                  "E" INTEGER ,
                  "F" INTEGER ,
                  "G" INTEGER ,
                  "H" INTEGER ,
                  "I" INTEGER ,
                  "J" INTEGER )
                 COMPRESS YES
                 DISTRIBUTE BY HASH("DATE_ID")
                   IN "USERSPACE1" ;

ALTER TABLE "ODS     "."PK10"
        ADD PRIMARY KEY
                ("DATE_ID",
                 "CODE_ID");

创建存储过程日志表

CREATE TABLE "SMY     "."SMY_LOG"  (
                  "SMY_PROC_NM" VARCHAR(100) NOT NULL ,
                  "SMY_ACT_DT" DATE NOT NULL ,
                  "SMY_STEPNUM" INTEGER NOT NULL ,
                  "SMY_STEPDESC" VARCHAR(100) ,
                  "SMY_SQLCODE" INTEGER ,
                  "SMY_RCOUNT" INTEGER ,
                  "CUR_TS" TIMESTAMP )
                 COMPRESS YES
                 DISTRIBUTE BY HASH("SMY_PROC_NM")
                   IN "USERSPACE1" ;

创建sor表

CREATE TABLE "SOR     "."PK10"  (
                  "DATE_ID" DATE NOT NULL ,
                  "CODE_ID" INTEGER NOT NULL ,
                  "TP_ID" CHAR(2) NOT NULL ,
                  "VAL" INTEGER )
                 COMPRESS YES
                 DISTRIBUTE BY HASH("DATE_ID",
                 "CODE_ID")
                   IN "USERSPACE1" ;

ALTER TABLE "SOR     "."PK10"
        ADD PRIMARY KEY
                ("DATE_ID",
                 "CODE_ID",
                 "TP_ID");

 

创建sor龙虎表

CREATE TABLE "SOR     "."PK10_VS"  (
                  "DATE_ID" DATE NOT NULL ,
                  "CODE_ID" INTEGER NOT NULL ,
                  "TP_ID" CHAR(2) NOT NULL ,
                  "FIR_VAL" INTEGER ,
                  "LST_VAL" INTEGER ,
                  "VS" CHAR(2) )
                 DISTRIBUTE BY HASH("DATE_ID",
                 "CODE_ID",
                 "TP_ID")
                   IN "USERSPACE1" ;


-- DDL Statements for Primary Key on Table "SOR     "."PK10_VS"

ALTER TABLE "SOR     "."PK10_VS"
        ADD PRIMARY KEY
                ("DATE_ID",
                 "CODE_ID",
                 "TP_ID");

 

创建ods存储过程

CREATE OR REPLACE  PROCEDURE SMY.PROC_PK10(IN ACCOUNTING_DATE DATE)
LANGUAGE SQL
SPECIFIC PROC_PK10
--------------------------------------------------------
--功能描述:测试存储过程
--输入:
--源表:
--
--目标表:
--作者: 
--
--
--------------------------------------------------------
BEGIN
/*声明异常处理使用变量*/
DECLARE SQLCODE, SMY_SQLCODE INT DEFAULT 0;            --SQLCODE
DECLARE SMY_STEPNUM INT DEFAULT 0;                     --过程内部位置标记
DECLARE SMY_STEPDESC VARCHAR(100) DEFAULT ‘‘;          --过程内部位置描述
DECLARE SMY_DATE DATE;                                 --临时日期变量
DECLARE SMY_RCOUNT INT;                                --DML语句作用记录数
DECLARE SMY_PROCNM VARCHAR(100);                       --存储过程名称
DECLARE AT_END SMALLINT DEFAULT 0;                     --SQL查询结果结束标志

/*变量赋值*/
--SET SMY_PROCNM = ‘PROC_CORE_BGFMCINF‘;
SET AT_END = 1;
SET SMY_DATE = ACCOUNTING_DATE;
SET SMY_PROCNM = PROC_PK10;
SET SMY_DATE=ACCOUNTING_DATE;
--call dbms_output.put_line(AT_END);
call dbms_output.put_line(SMY_PROCNM);
call dbms_output.put_line(to_char(varchar(current date),YYYY-MM-DD)||  ||varchar(current time));

/*删除日志表,条件:SMY_PROCNM=当前存储过程名字,SMY_DATE=ACCOUNTING_DATE,并插入新的起始标志*/
DELETE FROM SMY.SMY_LOG WHERE SMY_ACT_DT = SMY_DATE AND SMY_PROC_NM = SMY_PROCNM;
INSERT INTO SMY.SMY_LOG VALUES(SMY_PROCNM, SMY_DATE, SMY_STEPNUM, PROCEDURE START..., SMY_SQLCODE, SMY_RCOUNT, CURRENT TIMESTAMP);

/*STEP.1 : 删除目标表数据 */
SET SMY_STEPNUM = SMY_STEPNUM + 1 ;
SET SMY_STEPDESC = 删除目标表数据 ;
DELETE  FROM ODS.PK10  WHERE DATE_ID = ACCOUNTING_DATE;

GET DIAGNOSTICS SMY_RCOUNT = ROW_COUNT;
INSERT INTO SMY.SMY_LOG VALUES(SMY_PROCNM, SMY_DATE, SMY_STEPNUM, SMY_STEPDESC, SMY_SQLCODE ,SMY_RCOUNT, CURRENT TIMESTAMP);--
COMMIT;
call dbms_output.put_line(SMY_STEPNUM||.Delete ODS.PK10 WHERE DATE_ID = ||ACCOUNTING_DATE|| number is : ||SMY_RCOUNT);

/*STEP.2 :将SRC表数据merge进ODS表中 */
SET SMY_STEPNUM = SMY_STEPNUM + 1 ;
SET SMY_STEPDESC = 将SRC表数据merge进ODS表中 ;

MERGE INTO ODS.PK10 T
USING (SELECT * FROM SRC.PK10) S
ON T.CODE_ID=S.CODE_ID AND T.DATE_ID = S.DATE_ID
WHEN NOT MATCHED THEN 
INSERT (DATE_ID,CODE_ID,A,B,C,D,E,F,G,H,I,J)
VALUES(DATE_ID,CODE_ID,A,B,C,D,E,F,G,H,I,J);

GET DIAGNOSTICS SMY_RCOUNT = ROW_COUNT;
INSERT INTO SMY.SMY_LOG VALUES(SMY_PROCNM, SMY_DATE, SMY_STEPNUM, SMY_STEPDESC, SMY_SQLCODE ,SMY_RCOUNT, CURRENT TIMESTAMP);--
COMMIT;
call dbms_output.put_line(SMY_STEPNUM||.MERGE INTO ODS.PK10 WHERE DATE_ID = ||ACCOUNTING_DATE|| number is : ||SMY_RCOUNT);

/*STEP.3 :将ODS表数据进SOR表中 */
SET SMY_STEPNUM = SMY_STEPNUM + 1 ;
SET SMY_STEPDESC = 将ODS表数据进SOR表中 ;

MERGE INTO SOR.PK10 T
USING (
    SELECT CODE_ID,TP_ID,VAL,DATE_ID FROM (
        SELECT  DATE_ID, CODE_ID,A AS TP_ID ,A AS VAL FROM ODS.PK10
        UNION ALL                          
        SELECT DATE_ID, CODE_ID,B AS TP_ID ,B AS VAL FROM ODS.PK10
        UNION ALL                           
        SELECT DATE_ID, CODE_ID,C AS TP_ID ,C AS VAL FROM ODS.PK10
        UNION ALL                           
        SELECT DATE_ID, CODE_ID,D AS TP_ID ,D AS VAL FROM ODS.PK10
        UNION ALL                          
        SELECT DATE_ID, CODE_ID,E AS TP_ID ,E AS VAL FROM ODS.PK10
        UNION ALL                            
        SELECT DATE_ID, CODE_ID,F AS TP_ID ,F AS VAL FROM ODS.PK10
        UNION ALL                           
        SELECT DATE_ID, CODE_ID,G AS TP_ID,G AS VAL  FROM ODS.PK10
        UNION ALL                         
        SELECT DATE_ID, CODE_ID,H AS TP_ID,H AS VAL  FROM ODS.PK10
        UNION ALL                        
        SELECT DATE_ID, CODE_ID,I AS TP_ID,I AS VAL  FROM ODS.PK10
        UNION ALL                          
        SELECT DATE_ID, CODE_ID,J AS TP_ID,J AS VAL  FROM ODS.PK10
    )
) S
ON T.DATE_ID = S.DATE_ID AND T.CODE_ID=S.CODE_ID AND T.TP_ID=S.TP_ID
WHEN NOT MATCHED THEN 
INSERT (DATE_ID,CODE_ID,TP_ID,VAL)
VALUES(DATE_ID,CODE_ID,TP_ID,VAL);

GET DIAGNOSTICS SMY_RCOUNT = ROW_COUNT;
INSERT INTO SMY.SMY_LOG VALUES(SMY_PROCNM, SMY_DATE, SMY_STEPNUM, SMY_STEPDESC, SMY_SQLCODE ,SMY_RCOUNT, CURRENT TIMESTAMP);--
COMMIT;
call dbms_output.put_line(SMY_STEPNUM||.MERGE INTO SOR.PK10 WHERE DATE_ID = ||ACCOUNTING_DATE|| number is : ||SMY_RCOUNT);



/*STEP.3 :将ODS表数据进SOR龙虎表中*/
SET SMY_STEPNUM = SMY_STEPNUM + 1 ;
SET SMY_STEPDESC = 将ODS表数据进SOR龙虎表中 ;

MERGE INTO SOR.PK10_VS T
USING (
    SELECT DATE_ID,CODE_ID,TP_ID,FIR_VAL,LST_VAL,VS FROM (
        SELECT  DATE_ID, CODE_ID,A AS TP_ID ,A AS FIR_VAL,J AS LST_VAL ,
        CASE WHEN A>J THEN  ELSE  END AS VS FROM ODS.PK10
        UNION ALL                          
        SELECT DATE_ID, CODE_ID,B AS TP_ID ,B AS FIR_VAL, I AS LST_VAL ,
        CASE WHEN B>I THEN  ELSE  END AS VS FROM ODS.PK10
        UNION ALL                           
        SELECT DATE_ID, CODE_ID,C AS TP_ID ,C AS FIR_VAL, H AS LST_VAL,
        CASE WHEN C>H THEN  ELSE  END AS VS FROM ODS.PK10
        UNION ALL                           
        SELECT DATE_ID, CODE_ID,D AS TP_ID ,D AS FIR_VAL, G AS LST_VAL,
        CASE WHEN D>G THEN  ELSE  END AS VS FROM ODS.PK10
        UNION ALL                          
        SELECT DATE_ID, CODE_ID,E AS TP_ID ,E AS FIR_VAL,F AS LST_VAL,
        CASE WHEN E>F THEN  ELSE  END AS VS FROM ODS.PK10
    )
) S
ON T.DATE_ID = S.DATE_ID AND T.CODE_ID=S.CODE_ID AND T.TP_ID=S.TP_ID
WHEN NOT MATCHED THEN 
INSERT (DATE_ID,CODE_ID,TP_ID,FIR_VAL,LST_VAL,VS)
VALUES(DATE_ID,CODE_ID,TP_ID,FIR_VAL,LST_VAL,VS);

GET DIAGNOSTICS SMY_RCOUNT = ROW_COUNT;
INSERT INTO SMY.SMY_LOG VALUES(SMY_PROCNM, SMY_DATE, SMY_STEPNUM, SMY_STEPDESC, SMY_SQLCODE ,SMY_RCOUNT, CURRENT TIMESTAMP);--
COMMIT;

call dbms_output.put_line(SMY_STEPNUM||.MERGE INTO SOR.PK10_VS WHERE DATE_ID = ||ACCOUNTING_DATE|| number is : ||SMY_RCOUNT);
END@

 

北京PK10数据分析项目