-- MOPDB
INSERT INTO CT_MRDS.A_TASK_HISTORY (
	A_TASK_HISTORY_KEY,
	A_WORKFLOW_HISTORY_KEY,
	TASK_RUN_ID,
	TASK_NAME,
	TASK_START,
	TASK_END,
	TASK_SUCCESSFUL,
	SERVICE_NAME
)
SELECT 
    A_LOAD_SUBPROCESS_KEY AS A_TASK_HISTORY_KEY, 
    A_MOPDB_LOAD_SET_FK AS A_WORKFLOW_HISTORY_KEY, 
    CAST(NULL AS VARCHAR2(200)) AS TASK_RUN_ID, 
    CASE 
        WHEN SUBPROCESS_NAME = 'm_MOPDB_CSDB_DEBT_OU_CSDB_DEBT_LOAD' 
        THEN 'm_MOPDB_CSDB_DEBT_OU_CSDB_DEBT' 
        WHEN SUBPROCESS_NAME = 'm_MOPDB_CSDB_DEBT_DAILY_OU_CSDB_DEBT_DAILY_LOAD' 
        THEN 'm_MOPDB_CSDB_DEBT_DAILY_OU_CSDB_DEBT_DAILY'
        WHEN SUBPROCESS_NAME = 'm_MOPDB_CSDB_INSTR_DESC_FULL_OU_CSDB_INSTR_DESC_FULL_LOAD' 
        THEN 'm_MOPDB_CSDB_INSTR_DESC_FULL_OU_CSDB_INSTR_DESC_FULL' 
        WHEN SUBPROCESS_NAME = 'm_MOPDB_CSDB_ISSUER_DESC_FULL_OU_CSDB_ISSUER_DESC_FULL_LOAD' 
        THEN 'm_MOPDB_CSDB_ISSUER_DESC_FULL_OU_CSDB_ISSUER_DESC_FULL' 
        ELSE SUBPROCESS_NAME 
    END AS TASK_NAME, 
    PROCESS_START AS TASK_START, 
    PROCESS_END AS TASK_END, 
    PROCESS_SUCCESSFUL AS TASK_SUCCESSFUL, 
    'MOPDB' AS SERVICE_NAME 
FROM CT_MOPDB.A_LOAD_SUBPROCESS 
WHERE 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
    ')
);

COMMIT;	

