Your process log has several workflows broken step by numbers with the possible status values of Complete , Running or Error.
Your task is write SQL statement that creates overall status based upon the following requirements.
1. If all the workflow steps have a status of complete , set overall status as complete. (ex. Bravo)
2. If all the workflow steps have a status of Error , set overall status as Error. (ex. Foxtrot)
3. If all the workflow steps have the combination of error and complete , set overall status should be Indeterminate. (ex. Alpha , Charlie and Echo)
4. If all the workflow steps have the combination of Running and complete , set overall status should be Running. (ex. Delta)
Source:
Output:
WITH PROCESS_LOG AS (
SELECT 'Alpha' Workflow,1 stepno, 'Error' Status FROM DUAL
UNION ALL
SELECT 'Alpha' Workflow,2 stepno, 'Complete' Status FROM DUAL
UNION ALL
SELECT 'Bravo' Workflow,1 stepno, 'Complete' Status FROM DUAL
UNION ALL
SELECT 'Bravo' Workflow,2 stepno, 'Complete' Status FROM DUAL
UNION ALL
SELECT 'Charlie' Workflow,1 stepno, 'Complete' Status FROM DUAL
UNION ALL
SELECT 'Charlie' Workflow,2 stepno, 'Error' Status FROM DUAL
UNION ALL
SELECT 'Delta' Workflow,1 stepno, 'Complete' Status FROM DUAL
UNION ALL
SELECT 'Delta' Workflow,2 stepno, 'Running' Status FROM DUAL
UNION ALL
SELECT 'Echo' Workflow,1 stepno, 'Running' Status FROM DUAL
UNION ALL
SELECT 'Echo' Workflow,2 stepno, 'Error' Status FROM DUAL
UNION ALL
SELECT 'Foxtrot' Workflow,1 stepno, 'Error' Status FROM DUAL)
--decode(status,'Error','Indetermine',status)
select workflow,status from (
select workflow,
case when status='Error'
and count(rnk) over (partition by workflow) > 1
then 'Indetermine' else status end Status,
rnk
from (
SELECT WORKFLOW, STEPNO, STATUS,
DENSE_RANK() OVER (PARTITION BY WORKFLOW ORDER BY
CASE WHEN STATUS='Error' then 1
When Status = 'Running' then 2
when status='Complete' then 3 end,stepno) rnk
FROM PROCESS_LOG))
where rnk = 1
This solution only works for this particular dataset and not any general dataset which needs to satisfy the conditions given in question. for example if we add one more record for (Foxtron,2,Error) ideally it should show Error as overall state but it shows Indetermine.
ReplyDeletehere is my possible solution:
select workflow,case when ma=3 and mi in (1,2)
then "Indetermine"
when ma=1 then "Complete"
when ma=2 then "Running"
else "Error"
end as overall_status from (
select workflow,max(status_score) ma,min(status_score) mi from
(select *,
case when status="Error" then 3
when status="Running" then 2
when status="Complete" then 1
end as status_score from state) a
group by workflow) b;
note: its for MySQL