79491547

Date: 2025-03-07 07:43:39
Score: 0.5
Natty:
Report link

As @siggwemannen suggested I change the CTE query to the following, which fixed the issue:

;WITH cte
AS (SELECT --dtfs.ID,
           --dtfs.Downtime_ID,
        dtfs.Downtime_Event,
        dtfs.Func_Loc_ID,
        dtfs.Discipline_ID,
        dtfs.Activity_ID,
        dtfs.Reason_ID,
        dtfs.SUB_ID,
        dtfs.Duration,
        dtfs.Date_ID_Down,
        dtfs.Time_Down,
        dtfs.Date_ID_Up,
        dtfs.Time_Up,
        dtfs.Comments,
        dtfs.Engine_Hours,
        dtfs.Work_Order_Nbr,
        dtfs.Deleted_By,
        dtfs.Captured_By,
        dtfs.Booked_Up_By,
        dtfs.Approved_By,
        dtfs.Date_Captured,
        dtfs.Scada_Indicator,
        dtfs.Dispatch_Indicator,
        dtfs.InterlockId
    FROM @DowntimeFact dtfs
    WHERE dtfs.Downtime_Event > 1
    UNION ALL
    SELECT --dtfs.ID,
           --dtfs.Downtime_ID,
        Downtime_Event,
        Func_Loc_ID,
        Discipline_ID,
        Activity_ID,
        Reason_ID,
        SUB_ID,
        Duration,
        Date_ID_Down,
        Time_Down,
        Date_ID_Up + 1,
        Time_Up,
        Comments,
        Engine_Hours,
        Work_Order_Nbr,
        Deleted_By,
        Captured_By,
        Booked_Up_By,
        Approved_By,
        Date_Captured,
        Scada_Indicator,
        Dispatch_Indicator,
        InterlockId
    FROM CTE
    WHERE CTE.Downtime_Event > 1
          AND Date_ID_Down > Date_ID_Up)
SELECT cte.Downtime_Event,
       cte.Func_Loc_ID,
       cte.Discipline_ID,
       cte.Activity_ID,
       cte.Reason_ID,
       cte.SUB_ID,
       cte.Duration,
       cte.Date_ID_Down,
       cte.Time_Down,
       cte.Date_ID_Up,
       cte.Time_Up,
       cte.Comments,
       cte.Engine_Hours,
       cte.Work_Order_Nbr,
       cte.Deleted_By,
       cte.Captured_By,
       cte.Booked_Up_By,
       cte.Approved_By,
       cte.Date_Captured,
       cte.Scada_Indicator,
       cte.Dispatch_Indicator,
       cte.InterlockId
FROM cte
ORDER BY cte.Downtime_Event,
         cte.Date_ID_Up;
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @siggwemannen
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Danie Schoeman