@@ -587,63 +587,71 @@ CREATE TABLE #ProcessLog
587587(
588588Workflow VARCHAR (100 ),
589589StepNumber INTEGER ,
590- [Status] VARCHAR (100 ),
590+ RunStatus VARCHAR (100 ),
591591PRIMARY KEY (Workflow, StepNumber)
592592);
593593GO
594594
595- INSERT INTO #ProcessLog VALUES
596- (' Alpha' ,1 ,' Error' ),(' Alpha' ,2 ,' Complete' ),(' Bravo' ,1 ,' Complete' ),(' Bravo' ,2 ,' Complete' ),
597- (' Charlie' ,1 ,' Complete' ),(' Charlie' ,2 ,' Error' ),(' Delta' ,1 ,' Complete' ),(' Delta' ,2 ,' Running' ),
598- (' Echo' ,1 ,' Running' ),(' Echo' ,2 ,' Error' ),(' Foxtrot' ,1 ,' Error' ),(' Foxtrot' ,2 ,' Error' );
599- GO
600-
601- -- Create a StatusRank table to solve the problem
602- DROP TABLE IF EXISTS #StatusRank;
595+ INSERT INTO ##ProcessLog VALUES
596+ (' Alpha' ,1 ,' Error' ),(' Alpha' ,2 ,' Complete' ),(' Alpha' ,3 ,' Running' ),
597+ (' Bravo' ,1 ,' Complete' ),(' Bravo' ,2 ,' Complete' ),
598+ (' Charlie' ,1 ,' Running' ),(' Charlie' ,2 ,' Running' ),
599+ (' Delta' ,1 ,' Error' ),(' Delta' ,2 ,' Error' ),
600+ (' Echo' ,1 ,' Running' ),(' Echo' ,2 ,' Complete' );
603601GO
604602
605- CREATE TABLE #StatusRank
603+ -- Solution 1
604+ -- MIN and MAX
605+ WITH cte_MinMax AS
606606(
607- [Status] VARCHAR (100 ),
608- [Rank] INTEGER ,
609- PRIMARY KEY ([Status], [Rank])
610- );
611- GO
612-
613- INSERT INTO #StatusRank VALUES
614- (' Error' ,1 ),
615- (' Running' ,2 ),
616- (' Complete' ,3 );
607+ SELECT Workflow,
608+ MIN (RunStatus) AS MinStatus,
609+ MAX (RunStatus) AS MaxStatus
610+ FROM ##ProcessLog
611+ GROUP BY Workflow
612+ ),
613+ cte_Error AS
614+ (
615+ SELECT
616+ Workflow,
617+ MAX (CASE RunStatus WHEN ' Error' THEN RunStatus END ) AS ErrorState,
618+ MAX (CASE RunStatus WHEN ' Running' THEN RunStatus END ) AS RunningState
619+ FROM ##ProcessLog
620+ WHERE RunStatus IN (' Error' ,' Running' )
621+ GROUP BY Workflow
622+ )
623+ SELECT a .Workflow ,
624+ CASE WHEN a .MinStatus = a .MaxStatus THEN a .MinStatus
625+ WHEN b .ErrorState = ' Error' THEN ' Indeterminate'
626+ WHEN b .RunningState = ' Running' THEN b .RunningState END AS RunStatus
627+ FROM cte_MinMax a LEFT OUTER JOIN
628+ cte_Error b on a .WorkFlow = b .WorkFlow
629+ ORDER BY 1 ;
617630GO
618631
619- WITH cte_CountExistsError AS
632+ -- Solution 2
633+ -- COUNT and STRING_AGG
634+ WITH cte_Distinct AS
620635(
621- SELECT Workflow, COUNT (DISTINCT [Status]) AS DistinctCount
622- FROM #ProcessLog a
623- WHERE EXISTS (SELECT 1
624- FROM #ProcessLog b
625- WHERE [Status] = ' Error' AND a .Workflow = b .Workflow )
626- GROUP BY Workflow
636+ SELECT DISTINCT
637+ Workflow,
638+ RunStatus
639+ FROM ##ProcessLog
627640),
628- cte_ErrorWorkflows AS
641+ cte_StringAgg AS
629642(
630- SELECT a . Workflow ,
631- ( CASE WHEN DistinctCount > 1 THEN ' Indeterminate' ELSE a.[Status] END ) AS [Status]
632- FROM #ProcessLog a INNER JOIN
633- cte_CountExistsError b ON a . WorkFlow = b . WorkFlow
634- GROUP BY a . WorkFlow , ( CASE WHEN DistinctCount > 1 THEN ' Indeterminate' ELSE a.[Status] END )
643+ SELECT Workflow,
644+ STRING_AGG (RunStatus, ' , ' ) as RunStatus_Agg,
645+ COUNT ( DISTINCT RunStatus) AS DistinctCount
646+ FROM cte_Distinct
647+ GROUP BY Workflow
635648)
636- SELECT DISTINCT
637- a .Workflow ,
638- FIRST_VALUE(a.[Status]) OVER (PARTITION BY a .Workflow ORDER BY b.[Rank]) AS [Status]
639- FROM #ProcessLog a INNER JOIN
640- #StatusRank b ON a.[Status] = b.[Status]
641- WHERE a .Workflow NOT IN (SELECT Workflow FROM cte_ErrorWorkflows)
642- UNION
643649SELECT Workflow,
644- [Status]
645- FROM cte_ErrorWorkflows
646- ORDER BY a .Workflow ;
650+ CASE WHEN DistinctCount = 1 THEN RunStatus_Agg
651+ WHEN RunStatus_Agg LIKE ' %Error%' THEN ' Indeterminate'
652+ WHEN RunStatus_Agg LIKE ' %Running%' THEN ' Running' END AS RunStatus
653+ FROM cte_StringAgg
654+ ORDER BY 1 ;
647655GO
648656
649657/* ----------------------------------------------------
0 commit comments