--Author: Venkata Potluri
--Date: May 07, 2015

-- Note: Generate this report only after initial population of the P_INT_S_LOY_RDM_ITM

DECLARE
   LOG_ROW_ID   NUMBER := 0;
   ROW_COUNT    NUMBER := 0;
BEGIN
   SELECT CASE WHEN MAX (ROW_ID) IS NOT NULL THEN MAX (ROW_ID) + 1 ELSE 1 END
     INTO LOG_ROW_ID
     FROM SIEBEL_PURGE.PURGE_LOG;

   INSERT INTO SIEBEL_PURGE.PURGE_LOG (ROW_ID, PROCESS_NAME, START_TIME)
      SELECT LOG_ROW_ID, 'Populate RDM_REPORT', SYSDATE FROM DUAL;

   COMMIT;

   --48 sces
   EXECUTE IMMEDIATE 'CREATE INDEX SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM_F1
   ON SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM (RDM_ID)
   PARALLEL 5';

   EXECUTE IMMEDIATE 'ALTER INDEX SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM_F1
   NOPARALLEL';

   --DROP TABLE siebel_purge.TEMP_RDM_REPORT_S1;

   --13:05 mins
   EXECUTE IMMEDIATE
      'CREATE TABLE SIEBEL_PURGE.TEMP_RDM_REPORT_S1
    AS
   (  SELECT /*+ parallel(A,5) full(a)*/
            A.TXN_ID, COUNT (*) ACTUAL_RDM_COUNT
        FROM SIEBEL.S_LOY_RDM_ITM A
       WHERE A.TXN_ID IN
                (SELECT /*+ index_ffs(P,P_INT_S_LOY_RDM_ITM_F1) */
                       R.TXN_ID
                   FROM SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM P
                        INNER JOIN SIEBEL.S_LOY_RDM_ITM R
                           ON R.ROW_ID = P.RDM_ID)
    GROUP BY A.TXN_ID)';

   --2 secs
   EXECUTE IMMEDIATE
      'CREATE INDEX SIEBEL_PURGE.TEMP_RDM_REPORT_S1_F1
   ON SIEBEL_PURGE.TEMP_RDM_REPORT_S1 (ACTUAL_RDM_COUNT, TXN_ID)
   PARALLEL 5';

   EXECUTE IMMEDIATE 'ALTER INDEX SIEBEL_PURGE.TEMP_RDM_REPORT_S1_F1
   NOPARALLEL';

   --DROP TABLE siebel_purge.TEMP_RDM_REPORT_S2;

   --1:52 mins
   EXECUTE IMMEDIATE
      'CREATE TABLE SIEBEL_PURGE.TEMP_RDM_REPORT_S2
    AS
   (  SELECT /*+ index_ffs(P,P_INT_S_LOY_RDM_ITM_F1) parallel(r,5) full(r) */
            R.TXN_ID, COUNT (*) PURGE_ACRL_RDM_COUNT
        FROM SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM P
             INNER JOIN SIEBEL.S_LOY_RDM_ITM R ON R.ROW_ID = P.RDM_ID
    GROUP BY R.TXN_ID)';

   --2 sces
   EXECUTE IMMEDIATE
      'CREATE INDEX SIEBEL_PURGE.TEMP_RDM_REPORT_S2_F1
   ON SIEBEL_PURGE.TEMP_RDM_REPORT_S2 (PURGE_ACRL_RDM_COUNT, TXN_ID)
   PARALLEL 5';

   EXECUTE IMMEDIATE 'ALTER INDEX SIEBEL_PURGE.TEMP_RDM_REPORT_S2_F1
   NOPARALLEL';

   --drop table siebel_purge.RDM_REPORT;

   --30 secs
   EXECUTE IMMEDIATE
      'CREATE TABLE SIEBEL_PURGE.RDM_REPORT
    AS
   SELECT /*+ parallel(m,5)  */
         M.ROW_ID MEM_ID,
          M.MEM_NUM,
          M.STATUS_CD MEM_STATUS,
          TIER.NAME TIER_NAME,
          T.ROW_ID TXN_ID,
          T.STATUS_CD TXN_STATUS,
          S3.RDM_TYPE
     FROM SIEBEL.S_LOY_TXN T
          INNER JOIN
          (SELECT /*+ index_ffs(s1,TEMP_RDM_REPORT_S1_F1) index_ffs(s2,TEMP_RDM_REPORT_S2_F1) parallel_index(s1,TEMP_RDM_REPORT_S1_F1,5) parallel_index(s2,TEMP_RDM_REPORT_S2_F1,5) */
                 S1.TXN_ID,
                  CASE
                     WHEN S1.ACTUAL_RDM_COUNT = S2.PURGE_ACRL_RDM_COUNT
                     THEN
                        ''All''
                     ELSE
                        ''Partial''
                  END
                     RDM_TYPE
             FROM SIEBEL_PURGE.TEMP_RDM_REPORT_S1 S1
                  INNER JOIN SIEBEL_PURGE.TEMP_RDM_REPORT_S2 S2
                     ON S1.TXN_ID = S2.TXN_ID) S3
             ON T.ROW_ID = S3.TXN_ID
          INNER JOIN SIEBEL.S_LOY_MEMBER M ON T.MEMBER_ID = M.ROW_ID
          INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID';

   ROW_COUNT := SQL%ROWCOUNT;

   EXECUTE IMMEDIATE 'DROP TABLE siebel_purge.TEMP_RDM_REPORT_S1';

   EXECUTE IMMEDIATE 'DROP TABLE siebel_purge.TEMP_RDM_REPORT_S2';

   EXECUTE IMMEDIATE
      'GRANT SELECT ON SIEBEL_PURGE.RDM_REPORT TO PURGE_RO_ROLE';

   UPDATE SIEBEL_PURGE.PURGE_LOG
      SET END_TIME = SYSDATE, PROCESSED_ROWS = ROW_COUNT
    WHERE ROW_ID = LOG_ROW_ID;

   COMMIT;
END;
/

QUIT