-- MOPDB
DELETE FROM CT_MRDS.A_TASK_HISTORY_SOURCE t WHERE EXISTS (
SELECT 1
FROM
    CT_MOPDB.A_LOAD_HISTORY_SOURCE r
WHERE
    r.SOURCE_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'
)
AND t.A_TASK_HISTORY_SOURCE_KEY             = r.A_LOAD_HISTORY_SOURCE_KEY
AND t.A_TASK_HISTORY_KEY                    = r.A_LOAD_SUBPROCESS_FK
AND NVL(t.A_WORKFLOW_HISTORY_SOURCE_KEY,0)  = NVL(r.A_ODS_LOAD_SET_FK,0)
AND NVL(t.SOURCE_NAME,' ')                  = NVL(CASE 
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_ADHOC_ADJ_HEADER'          THEN 'OU_LM_ADHOC_ADJUSTMENTS_HEADER'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_ADHOC_ADJ_ITEM'            THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_ADHOC_ADJ_ITEM_HEADER'     THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM_HEADER'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_ADHOC_ADJ_ITEM_HEADER'     THEN 'OU_LM_ADHOC_ADJUSTMENTS_ITEM_HEADER'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_CSM_ADJ_HEADER'            THEN 'OU_LM_CSM_ADJUSTMENTS_HEADER'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_CSM_ADJ_ITEM'              THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_CSM_ADJ_ITEM_HEADER'       THEN 'OU_LM_CSM_ADJUSTMENTS_ITEM_HEADER'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_STANDING_FACILITY'         THEN 'OU_LM_STANDING_FACILITIES'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_STANDING_FACILITY_HEADER'  THEN 'OU_LM_STANDING_FACILITIES_HEADER'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_MRR_MRR_IND_CURR_ACC_HEADER'  THEN 'OU_LM_CURRENT_ACCOUNTS_HEADER'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_MRR_MRR_IND_CURR_ACC_ITEM'    THEN 'OU_LM_CURRENT_ACCOUNTS_ITEM'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_QR_ADJ_HEADER'             THEN 'OU_LM_QRE_ADJUSTMENTS_HEADER'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_QR_ADJ_ITEM'               THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM'
                                                    WHEN r.SOURCE_TABLE_NAME  = 'OU_LM_QR_ADJ_ITEM_HEADER'        THEN 'OU_LM_QRE_ADJUSTMENTS_ITEM_HEADER'
                                                    ELSE r.SOURCE_TABLE_NAME  
                                                  END,' ')
AND NVL(t.ROW_COUNT,0)                      = NVL(r.ROW_COUNT,0)
AND t.SERVICE_NAME                          = 'MOPDB'
)
;

COMMIT;