-- ====================================================================
-- A_DWH_LOAD_HISTORY View Definition
-- ====================================================================
-- Purpose: View for DWH (RAR) load history with workflow and task tracking
-- Source: MARS-770 install_package_MarsGeneralRelease_MARS770_views.sql
-- ====================================================================

CREATE OR REPLACE FORCE EDITIONABLE VIEW "CT_MRDS"."A_DWH_LOAD_HISTORY" 
("A_WORKFLOW_HISTORY_KEY", "A_WORKFLOW_HISTORY_SOURCE_KEY", "A_TASK_HISTORY_KEY", 
 "TASK_NAME", "TASK_START", "TASK_END", "WORKFLOW_SUCCESSFUL") 
DEFAULT COLLATION "USING_NLS_COMP" AS 
SELECT DISTINCT wh.a_workflow_history_key,
                ths.a_workflow_history_source_key,
                th.a_task_history_key,
                th.task_name,
                th.task_start,
                th.task_end,
                wh.workflow_successful
FROM CT_MRDS.A_WORKFLOW_HISTORY wh
JOIN CT_MRDS.A_TASK_HISTORY th
  ON wh.a_workflow_history_key = th.a_workflow_history_key
  AND wh.service_name = th.service_name
JOIN CT_MRDS.A_TASK_HISTORY_SOURCE ths
  ON th.a_task_history_key = ths.a_task_history_key 
  AND th.service_name = ths.service_name
WHERE wh.service_name = 'RAR'
  AND wh.workflow_successful = 'Y'
  AND LOWER(th.task_name) NOT LIKE '%_sq'
  AND ths.a_workflow_history_source_key IS NOT NULL;