I had a look at the following questions (it allowed me to get to where I am stuck):
38986774
64371506
27489564
Updated: Table Definition:
DECLARE @DowntimeFact TABLE
(
[ID] INT IDENTITY(1, 1),
[Downtime_ID] INT,
[Downtime_Event] INT,
[Func_Loc_ID] INT,
[Discipline_ID] INT,
[Activity_ID] INT,
[Reason_ID] INT,
[SUB_ID] INT,
[Duration] INT,
[Date_ID_Down] INT,
[Time_Down] CHAR(10),
[Date_ID_Up] INT,
[Time_Up] CHAR(10),
[Comments] NVARCHAR(1000),
[Engine_Hours] BIGINT,
[Work_Order_Nbr] NVARCHAR(16),
[Deleted_By] NVARCHAR(100),
[Captured_By] NVARCHAR(100),
[Booked_Up_By] NVARCHAR(100),
[Approved_By] NVARCHAR(100),
[Date_Captured] DATETIME,
[Scada_Indicator] BIT,
[Dispatch_Indicator] BIT,
[InterlockId] INT
);
Here is the CTE Query current used to generate the result:
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,
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 + 1,
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
AND dtfs.Date_ID_Down > dtfs.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_Down;
Here is the source data the query is using on:
Downtime_Event Func_Loc_ID Discipline_ID Activity_ID Reason_ID SUB_ID Duration Date_ID_Down Time_Down Date_ID_Up Time_Up Comments Engine_Hours Work_Order_Nbr Deleted_By Captured_By Booked_Up_By Approved_By Date_Captured Scada_Indicator Dispatch_Indicator InterlockId
5070714 9024 1 2 159903 1 2189 7390 11:35:00 7389 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070715 9024 1 2 159903 1 2879 7391 00:05:00 7390 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070716 9024 1 2 159903 1 2879 7392 00:05:00 7391 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070717 9027 1 2 160263 2 43343 7432 07:05:00 7402 09:30:00 Fault find alternator Reason: AMS FAILURE Equpment Type: Dump Truck Equpment Description: Tonly TLD65 Dump Truck (40T) Service Area:Reclaim Conveyor MB 2 NULL 5300159488 NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
Here is the Results returned by the CTE query:
Downtime_Event Func_Loc_ID Discipline_ID Activity_ID Reason_ID SUB_ID Duration Date_ID_Down Time_Down Date_ID_Up Time_Up Comments Engine_Hours Work_Order_Nbr Deleted_By Captured_By Booked_Up_By Approved_By Date_Captured Scada_Indicator Dispatch_Indicator InterlockId
5070714 9024 1 2 159903 1 2189 7390 11:35:00 7389 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070714 9024 1 2 159903 1 2189 7390 11:35:00 7390 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070715 9024 1 2 159903 1 2879 7391 00:05:00 7391 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070715 9024 1 2 159903 1 2879 7391 00:05:00 7390 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070716 9024 1 2 159903 1 2879 7392 00:05:00 7391 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070716 9024 1 2 159903 1 2879 7392 00:05:00 7392 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070717 9027 1 2 160263 2 43343 7432 07:05:00 7403 09:30:00 Fault find alternator Reason: AMS FAILURE Equpment Type: Dump Truck Equpment Description: Tonly TLD65 Dump Truck (40T) Service Area:Reclaim Conveyor MB 2 NULL 5300159488 NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070717 9027 1 2 160263 2 43343 7432 07:05:00 7403 09:30:00 Fault find alternator Reason: AMS FAILURE Equpment Type: Dump Truck Equpment Description: Tonly TLD65 Dump Truck (40T) Service Area:Reclaim Conveyor MB 2 NULL 5300159488 NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 09:02:00.647 0 0 NULL
I am quite sure there is something wrong with the result as the last row in the source should generate 30 rows in the result.
What am I just not doing right?
I had a look at the following questions (it allowed me to get to where I am stuck):
38986774
64371506
27489564
Updated: Table Definition:
DECLARE @DowntimeFact TABLE
(
[ID] INT IDENTITY(1, 1),
[Downtime_ID] INT,
[Downtime_Event] INT,
[Func_Loc_ID] INT,
[Discipline_ID] INT,
[Activity_ID] INT,
[Reason_ID] INT,
[SUB_ID] INT,
[Duration] INT,
[Date_ID_Down] INT,
[Time_Down] CHAR(10),
[Date_ID_Up] INT,
[Time_Up] CHAR(10),
[Comments] NVARCHAR(1000),
[Engine_Hours] BIGINT,
[Work_Order_Nbr] NVARCHAR(16),
[Deleted_By] NVARCHAR(100),
[Captured_By] NVARCHAR(100),
[Booked_Up_By] NVARCHAR(100),
[Approved_By] NVARCHAR(100),
[Date_Captured] DATETIME,
[Scada_Indicator] BIT,
[Dispatch_Indicator] BIT,
[InterlockId] INT
);
Here is the CTE Query current used to generate the result:
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,
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 + 1,
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
AND dtfs.Date_ID_Down > dtfs.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_Down;
Here is the source data the query is using on:
Downtime_Event Func_Loc_ID Discipline_ID Activity_ID Reason_ID SUB_ID Duration Date_ID_Down Time_Down Date_ID_Up Time_Up Comments Engine_Hours Work_Order_Nbr Deleted_By Captured_By Booked_Up_By Approved_By Date_Captured Scada_Indicator Dispatch_Indicator InterlockId
5070714 9024 1 2 159903 1 2189 7390 11:35:00 7389 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070715 9024 1 2 159903 1 2879 7391 00:05:00 7390 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070716 9024 1 2 159903 1 2879 7392 00:05:00 7391 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070717 9027 1 2 160263 2 43343 7432 07:05:00 7402 09:30:00 Fault find alternator Reason: AMS FAILURE Equpment Type: Dump Truck Equpment Description: Tonly TLD65 Dump Truck (40T) Service Area:Reclaim Conveyor MB 2 NULL 5300159488 NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
Here is the Results returned by the CTE query:
Downtime_Event Func_Loc_ID Discipline_ID Activity_ID Reason_ID SUB_ID Duration Date_ID_Down Time_Down Date_ID_Up Time_Up Comments Engine_Hours Work_Order_Nbr Deleted_By Captured_By Booked_Up_By Approved_By Date_Captured Scada_Indicator Dispatch_Indicator InterlockId
5070714 9024 1 2 159903 1 2189 7390 11:35:00 7389 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070714 9024 1 2 159903 1 2189 7390 11:35:00 7390 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070715 9024 1 2 159903 1 2879 7391 00:05:00 7391 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070715 9024 1 2 159903 1 2879 7391 00:05:00 7390 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070716 9024 1 2 159903 1 2879 7392 00:05:00 7391 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070716 9024 1 2 159903 1 2879 7392 00:05:00 7392 00:05:00 Carry on with inspection 23/03/2023 - Received order only 25/03/2023 Reason: Brake inspection Equpment Type: Dump Truck Equpment Description: Tonly TLD90 Dump Truck (60T) Service Area:Hydro Mining NULL NULL NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070717 9027 1 2 160263 2 43343 7432 07:05:00 7403 09:30:00 Fault find alternator Reason: AMS FAILURE Equpment Type: Dump Truck Equpment Description: Tonly TLD65 Dump Truck (40T) Service Area:Reclaim Conveyor MB 2 NULL 5300159488 NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 08:49:28.023 0 0 NULL
5070717 9027 1 2 160263 2 43343 7432 07:05:00 7403 09:30:00 Fault find alternator Reason: AMS FAILURE Equpment Type: Dump Truck Equpment Description: Tonly TLD65 Dump Truck (40T) Service Area:Reclaim Conveyor MB 2 NULL 5300159488 NULL DTSystemAdmin DTSystemAdmin DTSystemAdmin 2025-03-07 09:02:00.647 0 0 NULL
I am quite sure there is something wrong with the result as the last row in the source should generate 30 rows in the result.
What am I just not doing right?
Share Improve this question edited Mar 7 at 7:21 Danie Schoeman asked Mar 7 at 7:11 Danie SchoemanDanie Schoeman 18810 bronze badges 3- 4 Your question is not greatly stated. Are all billions columns needed to understand it? I doubt it. Make a simple example of what you're trying to do and show the result you're looking for. But your recursive cte looks weird, if it's what you're trying to do that is. It doesn't join back on itself – siggemannen Commented Mar 7 at 7:22
- 1 @siggemannen Thank you very much. That did the trick. In my haste to get it done I missng basic stuff. Will you publish it as the answer – Danie Schoeman Commented Mar 7 at 7:29
- 1 I think it's fine, you solved it yourself so my answer won't really help anyone else. The question will probably gets closed anyway :) – siggemannen Commented Mar 7 at 7:36
1 Answer
Reset to default 1As @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;