-- 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,
  CASE
    WHEN WORKFLOW_NAME = 'w_MOPDB_LM_STANDING_FACILITY' THEN 'w_MOPDB_LM_STANDING_FACILITIES'
    ELSE WORKFLOW_NAME
  END 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_LM_ADHOC_ADJUSTMENT',
    'w_MOPDB_LM_BALANCESHEET',
    'w_MOPDB_LM_CSM_ADJUSTMENT',
    'w_MOPDB_LM_STANDING_FACILITY',
    'w_MOPDB_LM_CURRENT_ACCOUNTS',
    'w_MOPDB_LM_FORECAST',
    'w_MOPDB_LM_QRE_ADJUSTMENT',
    'w_MOPDB_LM_TTS'
)
;

-- 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                                 AS ORCHESTRATION_RUN_ID,
  CASE 
    WHEN WORKFLOW_NAME = 'w_ODS_LM_STANDING_FACILITY'         THEN 'w_ODS_LM_STANDING_FACILITIES'
    WHEN WORKFLOW_NAME = 'w_ODS_MRR_IND_CURRENT_ACCOUNT_ITEM' THEN 'w_ODS_LM_CURRENT_ACCOUNT_ITEM'
    WHEN WORKFLOW_NAME = 'w_ODS_QUARTERLY_ADJUSTMENT_MSG'     THEN 'w_ODS_LM_QUARTERLY_ADJUSTMENT_MSG'
    WHEN WORKFLOW_NAME = 'w_ODS_ADHOC_ADJUSTMENT_MSG'     THEN 'w_ODS_LM_ADHOC_ADJUSTMENTS_MSG'
    WHEN WORKFLOW_NAME = 'w_ODS_CSM_ADJUSTMENT_MSG'     THEN 'w_ODS_LM_CSM_ADJUSTMENT_MSG'
    ELSE WORKFLOW_NAME
  END                                 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_ADHOC_ADJUSTMENT_MSG',
    'w_ODS_LM_BALANCESHEET',
    'w_ODS_CSM_ADJUSTMENT_MSG',
    'w_ODS_LM_STANDING_FACILITY',
    'w_ODS_MRR_IND_CURRENT_ACCOUNT_ITEM',
    'w_ODS_QUARTERLY_ADJUSTMENT_MSG'
)
;

COMMIT;	

