--Author: Venkata Potluri
--Date: April 16, 2015
--Sqls to generate the test accounts as per the following scenarios.

--Accounts with transactions > 3 years old

SELECT DISTINCT M.ROW_ID MEMBER_ID,
                '' TXN_ID,
                M.MEM_NUM,
                M.STATUS_CD,
                TIER.NAME TIER_NAME,
                'Accounts with transactions > 3 years old' TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '80%'
                  OR M.MEM_NUM LIKE '81%'
                  OR M.MEM_NUM LIKE '82%'
                  OR M.MEM_NUM LIKE '83%'
                  OR M.MEM_NUM LIKE '84%')
             AND M.NAME NOT LIKE '%TESTER'
             AND T.TXN_DT < SYSDATE - 1100
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 4001;

--Accounts with transactions date > 3 years old with a processed on date <2 years old

SELECT M.ROW_ID MEMBER_ID,
       T.ROW_ID TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Accounts with transactions date > 3 years old with a processed on date <2 years old'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
--             AND (   M.MEM_NUM LIKE '75%'
--                  OR M.MEM_NUM LIKE '76%'
--                  OR M.MEM_NUM LIKE '77%'
--                  OR M.MEM_NUM LIKE '78%'
--                  OR M.MEM_NUM LIKE '79%')
             AND T.TXN_DT < SYSDATE - 1100
             AND T.PROCESS_DT > SYSDATE - 725
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 21;

--Accounts with transactions > 3 years old that have come from 2 separate accounts that have been merged into one

SELECT M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Accounts with transactions > 3 years old that have come from 2 separate accounts that have been merged into one'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_MEMBER M
       INNER JOIN SIEBEL.S_LOY_TIER TIER
          ON     M.PR_DOM_TIER_ID = TIER.ROW_ID
             AND M.ROW_ID IN
                    (SELECT MEMBER_ID
                       FROM (  SELECT T.MEMBER_ID, COUNT (T.X_ORIG_MBR_NBR)
                                 FROM SIEBEL.S_LOY_TXN T
                                      INNER JOIN SIEBEL.S_LOY_MEMBER M1
                                         ON     M1.ROW_ID = T.MEMBER_ID
                                            AND (   M1.MEM_NUM LIKE '70%'
                                                 OR M1.MEM_NUM LIKE '71%'
                                                 OR M1.MEM_NUM LIKE '72%'
                                                 OR M1.MEM_NUM LIKE '73%'
                                                 OR M1.MEM_NUM LIKE '74%')
                                            AND T.TXN_DT < SYSDATE - 1100
                                WHERE ROWNUM < 10001
                             GROUP BY T.MEMBER_ID
                               HAVING COUNT (T.X_ORIG_MBR_NBR) > 1));

--Accounts with transactions > 3 years old that have 2, 2008 conversion balance transactions

  SELECT M.MEM_NUM, COUNT (*)
    FROM SIEBEL_PURGE.TEMP_CNV_NOTES T
         INNER JOIN SIEBEL.S_LOY_MEMBER M ON T.MEMBER_ID = M.ROW_ID
GROUP BY M.MEM_NUM
  HAVING COUNT (*) > 1;

--Note: If TEMP_CNV_NOTES is not available or doesn't contain any data, then use the following SQL.

  /*SELECT M.ROW_ID MEMBER_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Accounts with transactions > 3 years old that have 2, 2008 conversion balance transactions' TEST_SCENARIO
  FROM SIEBEL.S_LOY_MEMBER M
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE M.ROW_ID IN (SELECT MEMBER_ID
                    FROM (  SELECT MEMBER_ID, COUNT (*)
                              FROM SIEBEL.S_LOY_TXN
                             WHERE PROD_ID =
                                      (SELECT ROW_ID
                                         FROM SIEBEL.S_PROD_INT
                                        WHERE NAME =
                                                 'CONVERSION BALANCE POINTS PRODUCT')
                          GROUP BY MEMBER_ID
                            HAVING COUNT (*) > 1));*/

--100 Accounts with Conversion balance

SELECT DISTINCT M.ROW_ID MEMBER_ID,
                '' TXN_ID,
                M.MEM_NUM,
                M.STATUS_CD,
                TIER.NAME TIER_NAME,
                '100 Accounts with Conversion balance' TEST_SCENARIO
  FROM SIEBEL_PURGE.TEMP_CNV_NOTES T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     T.MEMBER_ID = M.ROW_ID
             AND (   M.MEM_NUM LIKE '65%'
                  OR M.MEM_NUM LIKE '66%'
                  OR M.MEM_NUM LIKE '67%'
                  OR M.MEM_NUM LIKE '68%'
                  OR M.MEM_NUM LIKE '69%')
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 101;

--100 Accounts which don't have Conversion balance

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       '100 Accounts which don''t have Conversion balance' TEST_SCENARIO
  FROM SIEBEL.S_LOY_MEMBER M
       INNER JOIN SIEBEL.S_LOY_TIER TIER
          ON     M.PR_DOM_TIER_ID = TIER.ROW_ID
             AND (   M.MEM_NUM LIKE '60%'
                  OR M.MEM_NUM LIKE '61%'
                  OR M.MEM_NUM LIKE '62%'
                  OR M.MEM_NUM LIKE '63%'
                  OR M.MEM_NUM LIKE '64%')
             AND M.ROW_ID NOT IN (SELECT MEMBER_ID
                                    FROM SIEBEL_PURGE.TEMP_CNV_NOTES)
 WHERE ROWNUM < 101;

--Inactive accounts with transactions > 3 years old

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Inactive accounts with transactions > 3 years old' TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND M.STATUS_CD = 'Inactive'
             AND (   M.MEM_NUM LIKE '55%'
                  OR M.MEM_NUM LIKE '56%'
                  OR M.MEM_NUM LIKE '57%'
                  OR M.MEM_NUM LIKE '58%'
                  OR M.MEM_NUM LIKE '59%')
             AND T.TXN_DT < SYSDATE - 1100
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 401;

--Accounts with transfer transactions > 3 years old

SELECT M.ROW_ID MEMBER_ID,
       T.ROW_ID TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Accounts with transfer transactions > 3 years old' TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '50%'
                  OR M.MEM_NUM LIKE '51%'
                  OR M.MEM_NUM LIKE '52%'
                  OR M.MEM_NUM LIKE '53%'
                  OR M.MEM_NUM LIKE '54%')
             AND T.TXN_DT < SYSDATE - 1100
             AND T.TRANSFER_TXN_ID IS NOT NULL
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 51;

--Accounts with a transaction > 3 years old that is associated to a payment

SELECT M.ROW_ID MEMBER_ID,
       T.ROW_ID TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Accounts with a transaction > 3 years old that is associated to a payment'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '40%'
                  OR M.MEM_NUM LIKE '41%'
                  OR M.MEM_NUM LIKE '42%'
                  OR M.MEM_NUM LIKE '43%'
                  OR M.MEM_NUM LIKE '44%')
             AND T.ROW_ID IN
                    (SELECT X_SP_MEMBER_NUM
                       FROM SIEBEL.S_CONTACT_XM
                      WHERE TYPE = 'Payments' AND X_SP_MEMBER_NUM IS NOT NULL)
             AND T.TXN_DT < SYSDATE - 1100
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 101;

--EasyBiz accounts with transactions > 3 years old

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'EasyBiz accounts with transactions > 3 years old' TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '45%'
                  OR M.MEM_NUM LIKE '46%'
                  OR M.MEM_NUM LIKE '47%'
                  OR M.MEM_NUM LIKE '48%'
                  OR M.MEM_NUM LIKE '49%')
             AND T.TXN_DT < SYSDATE - 1100
             AND M.X_SOLAR_MEM_TYPE = 'EasyBiz'
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 20001;

--Charity accounts with transactions > 3 years old

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Charity accounts with transactions > 3 years old' TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND T.TXN_DT < SYSDATE - 1100
             AND M.X_SOLAR_MEM_TYPE = 'Charity'
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 200001;

--Million Mile accounts with transactions > 3 years old

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Million Mile accounts with transactions > 3 years old' TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '35%'
                  OR M.MEM_NUM LIKE '36%'
                  OR M.MEM_NUM LIKE '37%'
                  OR M.MEM_NUM LIKE '38%'
                  OR M.MEM_NUM LIKE '39%')
             AND M.X_MILLION_MILE_IND = 'Yes'
             AND T.TXN_DT < SYSDATE - 1100
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 20001;

--Club 49 members with transactions > 3 years old

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Club 49 members with transactions > 3 years old' TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '30%'
                  OR M.MEM_NUM LIKE '31%'
                  OR M.MEM_NUM LIKE '32%'
                  OR M.MEM_NUM LIKE '33%'
                  OR M.MEM_NUM LIKE '34%')
             AND T.TXN_DT < SYSDATE - 1100
             AND M.X_PROJ_MEMBER_TYPE = 'AK'
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 2001;

--Any accounts with transactions > 3 years old that have member attributes associated with the transactions

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Any accounts with transactions > 3 years old that have member attributes associated with the transactions'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_ATTR_ITM A
       INNER JOIN SIEBEL.S_LOY_ATTRIB B ON A.ATTRIB_ID = B.ROW_ID
       INNER JOIN SIEBEL.S_LOY_TXN T ON A.TXN_ID = T.ROW_ID
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '25%'
                  OR M.MEM_NUM LIKE '26%'
                  OR M.MEM_NUM LIKE '27%'
                  OR M.MEM_NUM LIKE '28%'
                  OR M.MEM_NUM LIKE '29%')
             AND T.TXN_DT < SYSDATE - 1100
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 201;

--Any accounts with transactions > 3 years old that have promotion attributes associated with the transactions

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Any accounts with transactions > 3 years old that have promotion attributes associated with the transactions'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_BCKT_TXN A
       INNER JOIN SIEBEL.S_LOY_TXN T ON A.TXN_ID = T.ROW_ID
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '20%'
                  OR M.MEM_NUM LIKE '21%'
                  OR M.MEM_NUM LIKE '22%'
                  OR M.MEM_NUM LIKE '23%'
                  OR M.MEM_NUM LIKE '24%')
             AND T.TXN_DT < SYSDATE - 1100
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 201;

--Any accounts with transactions > 3 years old that have Rejected-Engine status

SELECT DISTINCT
       M.ROW_ID MEMBER_ID,
       '' TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Any accounts with transactions > 3 years old that have Rejected-Engine status'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND T.TXN_DT < SYSDATE - 1100
             AND T.STATUS_CD = 'Rejected - Engine'
             AND (   M.MEM_NUM LIKE '15%'
                  OR M.MEM_NUM LIKE '16%'
                  OR M.MEM_NUM LIKE '17%'
                  OR M.MEM_NUM LIKE '18%'
                  OR M.MEM_NUM LIKE '19%')
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE ROWNUM < 101;

--Members with OCRA Transactions and Trasnaction > 3 years
--If txn_id has only one '-' then it is manual/Ocra
--If txn_id has two '-'s then it came through EIM and it is a PIT txn.

SELECT M.ROW_ID MEMBER_ID,
       T.ROW_ID TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Members with OCRA Transactions and Trasnaction > 3 years'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M
          ON     M.ROW_ID = T.MEMBER_ID
             AND (   M.MEM_NUM LIKE '85%'
                  OR M.MEM_NUM LIKE '86%'
                  OR M.MEM_NUM LIKE '87%'
                  OR M.MEM_NUM LIKE '88%'
                  OR M.MEM_NUM LIKE '89%')
             AND T.TXN_CHANNEL_CD = 'OCRA'
             AND T.TXN_DT < SYSDATE - 1100
             AND ROWNUM < 1001
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID;

--Members with 'SIGNATURE CARD $99 COMPANION TICKET AVAILABLE IN YOUR MY ACCOUNT' Transactions (eCerts)

SELECT M.ROW_ID MEMBER_ID,
       T.ROW_ID TXN_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Members with ''SIGNATURE CARD $99 COMPANION TICKET AVAILABLE IN YOUR MY ACCOUNT'' Transactions (eCerts)'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_TXN T
       INNER JOIN SIEBEL.S_LOY_MEMBER M ON M.ROW_ID = T.MEMBER_ID
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE     T.PROD_ID =
              (SELECT ROW_ID
                 FROM SIEBEL.S_PROD_INT
                WHERE NAME =
                         'SIGNATURE CARD $99 COMPANION TICKET AVAILABLE IN YOUR MY ACCOUNT')
       AND T.TXN_DT < SYSDATE - 1100
       AND (   M.MEM_NUM LIKE '10%'
            OR M.MEM_NUM LIKE '11%'
            OR M.MEM_NUM LIKE '12%'
            OR M.MEM_NUM LIKE '13%'
            OR M.MEM_NUM LIKE '14%')
       AND ROWNUM < 101;

--Any accounts that have a redemption (not eligible for purge) using accrued value of a transaction that will be getting purged
--Use "Generate_RDM_Report.sql" for this.

/*SELECT M.ROW_ID MEMBER_ID,
       M.MEM_NUM,
       M.STATUS_CD,
       TIER.NAME TIER_NAME,
       'Any accounts that have a redemption (not eligible for purge) using accrued value of a transaction that will be getting purged'
          TEST_SCENARIO
  FROM SIEBEL.S_LOY_MEMBER M
       INNER JOIN SIEBEL.S_LOY_TIER TIER ON M.PR_DOM_TIER_ID = TIER.ROW_ID
 WHERE M.ROW_ID IN
          (SELECT TA.MEMBER_ID
             FROM SIEBEL.S_LOY_ACRL_ITM A
                  INNER JOIN SIEBEL.S_LOY_TXN TA ON TA.ROW_ID = A.TXN_ID
            WHERE     A.ROW_ID IN
                         (SELECT R.ACCRUAL_ITEM_ID
                            FROM SIEBEL.S_LOY_RDM_ITM R
                                 INNER JOIN SIEBEL.S_LOY_TXN TR
                                    ON R.TXN_ID = TR.ROW_ID
                           WHERE TR.TXN_DT < SYSDATE - 1000)
                  AND TA.TXN_DT < SYSDATE - 1100
                  AND ROWNUM < 101);
*/

-- Jens' requests 2, 3, 4 can be achived from P_INT_S_LOY_RDM_ITM
--This report is moved to "Generate_RDM_Report.sql"
--and generate this report only after initial population of the P_INT_S_LOY_RDM_ITM
--or follow the sequence of the sqls.