-- MOPDB
INSERT INTO CT_MRDS.A_WORKFLOW_HISTORY (
	A_WORKFLOW_HISTORY_KEY,
	ORCHESTRATION_RUN_ID,
	WORKFLOW_NAME,
	WORKFLOW_START,
	WORKFLOW_END,
	WORKFLOW_SUCCESSFUL,
	DBT_INVOCATION_ID,
	SERVICE_NAME
) 
SELECT 
    A_MOPDB_LOAD_SET_KEY AS A_WORKFLOW_HISTORY_KEY,
    CAST(WLA_RUN_ID AS VARCHAR2(200)) AS ORCHESTRATION_RUN_ID,
    WORKFLOW_NAME  AS WORKFLOW_NAME ,
    LOAD_START as WORKFLOW_START,
    LOAD_END AS WORKFLOW_END,
    'Y'  AS WORKFLOW_SUCCESSFUL,
    CAST(INFA_RUN_ID AS VARCHAR2(200)) AS DBT_INVOCATION_ID,
    'MOPDB' AS SERVICE_NAME
FROM
    CT_MOPDB.A_LOAD_HISTORY 
WHERE
    WORKFLOW_NAME IN
      ('w_MOPDB_CSDB_REFERENCE_MONTHLY',
      'w_MOPDB_CSDB_REFERENCE_DAILY',
      'w_MOPDB_CSDB_RATINGS'
);

-- ODS
INSERT INTO CT_MRDS.A_WORKFLOW_HISTORY (
	A_WORKFLOW_HISTORY_KEY,
	ORCHESTRATION_RUN_ID,
	WORKFLOW_NAME,
	WORKFLOW_START,
	WORKFLOW_END,
	WORKFLOW_SUCCESSFUL,
	DBT_INVOCATION_ID,
	SERVICE_NAME
) 
SELECT 
    A_ETL_LOAD_SET_KEY AS A_WORKFLOW_HISTORY_KEY, 
    CASE WHEN WLA_RUN_ID is not null THEN CAST(WLA_RUN_ID AS VARCHAR2(200)) ELSE EXDI_CORRELATION_ID END ORCHESTRATION_RUN_ID,
    WORKFLOW_NAME AS WORKFLOW_NAME,
    LOAD_START AS WORKFLOW_START, 
    LOAD_END AS WORKFLOW_END, 
    LOAD_SUCCESSFUL AS workflow_successful, 
    CAST(INFA_RUN_ID AS VARCHAR2(200)) as DBT_INVOCATION_ID,
    'ODS' AS SERVICE_NAME
FROM CT_ODS.A_LOAD_HISTORY 
WHERE WORKFLOW_NAME IN ('w_ODS_CSDB_DEBT', 'w_ODS_CSDB_DEBT_DAILY', 'w_ODS_CSDB_RATINGS_FULL')
;

COMMIT;	

