A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
DECLARE @LOCAL_TABLEVARIABLE TABLE
(UniqueAppNum VARCHAR(1000),
ActionName VARCHAR(1000),
StartDate Datetime,
EndDate Datetime,
[StatusName] VARCHAR(100)
)
Insert into @LOCAL_TABLEVARIABLE values
( 'APP1','ACT1','2022-03-27 00:10:15.000' ,'2022-03-27 00:11:00.000' ,'Init' ),
( 'APP1','ACT2','2022-03-27 00:11:01.000' ,'2022-03-27 00:11:08.000' ,'ST2'),
( 'APP1','ACT3','2022-03-27 00:11:09.000' ,'2022-03-27 00:11:15.000' ,'ST3'),
( 'APP1','ACT4','2022-03-27 00:11:16.000' ,'2022-03-27 00:12:15.000' ,'Waiting'),
( 'APP1','ACT5','2022-03-27 00:12:15.000' ,'2022-03-27 00:12:33.000' ,'Waiting'),
( 'APP2','ACT1','2022-03-28 00:10:15.000' ,'2022-03-28 00:11:00.000' ,'Init' ),
( 'APP2','ACT2','2022-03-28 00:11:01.000' ,'2022-03-28 00:11:08.000' ,'ST2'),
( 'APP2','ACT3','2022-03-28 00:11:09.000' ,'2022-03-28 00:11:15.000' ,'ST3'),
( 'APP2','ACT4','2022-03-28 00:11:16.000' ,'2022-03-28 00:12:15.000' ,'Waiting')
SELECT UniqueAppNum,
Format(
Max(case when ActionName='Act2' then EndDate else null end)
-Max(case when ActionName='Act1' then StartDate else null end), 'HH:mm:ss') ACT2,
Format(
Max(case when ActionName='Act4' then EndDate else null end)
-Max(case when ActionName='Act2' then StartDate else null end), 'HH:mm:ss') ACT4,
Format(
Max(case when ActionName='Act5' then EndDate else null end)
-Max(case when ActionName='Act4' then StartDate else null end), 'HH:mm:ss') ACT6
FROM @LOCAL_TABLEVARIABLE
GROUP BY UniqueAppNum