-- MOPDB
DELETE FROM CT_MRDS.A_TASK_HISTORY_TARGET t WHERE EXISTS (
SELECT 1
FROM
    CT_MOPDB.A_LOAD_HISTORY_TARGET r
WHERE
    TARGET_TABLE_NAME IN (
    'LM_T_FC_ADH_ADJUSTMENTS',
    'LM_T_BALANCE_SHEET',
    'LM_T_FC_CSM_ADJUSTMENTS',
    'LM_T_STANDING_FACILITIES',
    'LM_T_CURRENT_ACCOUNTS',
    'LM_T_FORECAST',
    'LM_T_FC_QRE_ADJUSTMENTS',
    'LM_T_TTS'
)
AND t.A_TASK_HISTORY_TARGET_KEY = r.A_LOAD_HISTORY_TARGET_KEY
AND t.A_TASK_HISTORY_KEY        = r.A_LOAD_SUBPROCESS_FK
AND NVL(t.TARGET_NAME,' ')      = NVL(r.TARGET_TABLE_NAME,' ')
AND NVL(t.ROW_COUNT_APPLIED,0)  = NVL(r.ROW_COUNT_APPLIED,0)
AND NVL(t.ROW_COUNT_REJECTED,0) = NVL(r.ROW_COUNT_REJECTED,0)
AND NVL(t.LOAD_SUCCESSFUL,' ')  = NVL(r.LOAD_SUCCESSFUL,' ')
AND t.SERVICE_NAME              = 'MOPDB'
);

-- ODS
DELETE FROM CT_MRDS.A_TASK_HISTORY_TARGET t WHERE EXISTS (
  SELECT 1
  FROM
      CT_ODS.A_LOAD_HISTORY_TARGET r
  WHERE
      TARGET_TABLE_NAME IN (
      'OU_LM_ADHOC_ADJ_HEADER',
      'OU_LM_ADHOC_ADJ_ITEM',
      'OU_LM_ADHOC_ADJ_ITEM_HEADER',
      'OU_LM_BALANCESHEET_HEADER',
      'OU_LM_BALANCESHEET_ITEM',
      'OU_LM_CSM_ADJ_HEADER',
      'OU_LM_CSM_ADJ_ITEM',
      'OU_LM_CSM_ADJ_ITEM_HEADER',
      'OU_LM_STANDING_FACILITY',
      'OU_LM_STANDING_FACILITY_HEADER',
      'OU_MRR_MRR_IND_CURR_ACC_HEADER',
      'OU_MRR_MRR_IND_CURR_ACC_ITEM',
      'OU_LM_FORECAST_HEADER',
      'OU_LM_FORECAST_ITEM',
      'OU_LM_QR_ADJ_HEADER',
      'OU_LM_QR_ADJ_ITEM',
      'OU_LM_QR_ADJ_ITEM_HEADER',
      'OU_LM_TTS_HEADER',
      'OU_LM_TTS_ITEM',
      'CSM_ADJ_HEADER',
      'CSM_ADJ_ITEM',
      'CSM_ADJ_ITEM_HEADER',
      'QR_ADJ_HEADER',
      'QR_ADJ_ITEM',
      'QR_ADJ_ITEM_HEADER',
      'STANDING_FACILITY',
      'STANDING_FACILITY_HEADER'
  )
  AND t.A_TASK_HISTORY_TARGET_KEY = r.A_LOAD_HISTORY_TARGET_KEY
  AND t.A_TASK_HISTORY_KEY        = r.A_ETL_LOAD_SET_FK
  AND NVL(t.TARGET_NAME,' ')      = NVL(CASE 
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_ADHOC_ADJ_HEADER' THEN 'OU_LM_ADHOC_ADJUSTMENTS_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_ADHOC_ADJ_ITEM' THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_ADHOC_ADJ_ITEM_HEADER' THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_ADHOC_ADJ_ITEM_HEADER' THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_CSM_ADJ_HEADER' THEN 'OU_LM_CSM_ADJUSTMENTS_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'CSM_ADJ_HEADER' THEN 'OU_LM_CSM_ADJUSTMENTS_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_CSM_ADJ_ITEM' THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM'
                                          WHEN r.TARGET_TABLE_NAME = 'CSM_ADJ_ITEM' THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_CSM_ADJ_ITEM_HEADER' THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'CSM_ADJ_ITEM_HEADER' THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_STANDING_FACILITY' THEN 'OU_LM_STANDING_FACILITIES'
                                          WHEN r.TARGET_TABLE_NAME = 'STANDING_FACILITY' THEN 'OU_LM_STANDING_FACILITIES'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_STANDING_FACILITY_HEADER' THEN 'OU_LM_STANDING_FACILITIES_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'STANDING_FACILITY_HEADER' THEN 'OU_LM_STANDING_FACILITIES_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_MRR_MRR_IND_CURR_ACC_HEADER' THEN 'OU_LM_CURRENT_ACCOUNTS_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_MRR_MRR_IND_CURR_ACC_ITEM' THEN 'OU_LM_CURRENT_ACCOUNTS_ITEM'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_QR_ADJ_HEADER' THEN 'OU_LM_QRE_ADJUSTMENTS_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'QR_ADJ_HEADER' THEN 'OU_LM_QRE_ADJUSTMENTS_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_QR_ADJ_ITEM' THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM'
                                          WHEN r.TARGET_TABLE_NAME = 'QR_ADJ_ITEM' THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM'
                                          WHEN r.TARGET_TABLE_NAME = 'OU_LM_QR_ADJ_ITEM_HEADER' THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM_HEADER'
                                          WHEN r.TARGET_TABLE_NAME = 'QR_ADJ_ITEM_HEADER' THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM_HEADER'
                                          ELSE r.TARGET_TABLE_NAME 
                                        END,' ')
  AND NVL(t.ROW_COUNT_APPLIED,0)  = NVL(r.ROW_COUNT_APPLIED,0)
  AND NVL(t.ROW_COUNT_REJECTED,0) = NVL(r.ROW_COUNT_REJECTED,0)
  AND NVL(t.LOAD_SUCCESSFUL,' ')  = 'Y'
  AND t.SERVICE_NAME              = 'ODS'
);
   

COMMIT;