-- MOPDB
DELETE FROM CT_MRDS.A_TASK_HISTORY t WHERE EXISTS (
  SELECT 1
  FROM CT_MOPDB.A_LOAD_SUBPROCESS r
  WHERE 
      r.SUBPROCESS_NAME IN (
    SELECT DISTINCT SUBPROCESS_NAME 
    FROM CT_MOPDB.A_LOAD_HISTORY A
    LEFT JOIN CT_MOPDB.A_LOAD_SUBPROCESS B
    ON A.A_MOPDB_LOAD_SET_KEY = b.A_MOPDB_LOAD_SET_FK
    WHERE WORKFLOW_NAME IN (
      'w_MOPDB_CSDB_REFERENCE_DAILY',
      'w_MOPDB_CSDB_REFERENCE_MONTHLY',
      'w_MOPDB_CSDB_RATINGS'
    )
  )
	AND t.A_TASK_HISTORY_KEY     = r.A_LOAD_SUBPROCESS_KEY
	AND t.A_WORKFLOW_HISTORY_KEY = r.A_MOPDB_LOAD_SET_FK
	AND NVL(t.TASK_RUN_ID,' ')        = NVL(CAST(NULL AS VARCHAR2(200)),' ')
	AND NVL(t.TASK_NAME,' ')||'_LOAD'         = NVL(r.SUBPROCESS_NAME,' ') 
	AND NVL(t.TASK_START,SYSDATE)         = NVL(r.PROCESS_START,SYSDATE)
	AND NVL(t.TASK_END,SYSDATE)           = NVL(r.PROCESS_END,SYSDATE)
	AND NVL(t.TASK_SUCCESSFUL,' ')    = NVL(r.PROCESS_SUCCESSFUL,' ')
	AND t.SERVICE_NAME           = 'MOPDB' 
);

COMMIT;