I am trying to calculate the duration between two different times. Here is my Original query and its results:
SELECT
[DJ_NUMBER]
,[PROCESSING_ACTION]
,[TRANSACTION_QTY]
,[MES_MACHINE]
,[ACTION]
,[ACTION_TIME]
FROM [StagingDB].[dbo].[AFL_MES2O_Interface_Data_arch]
-- where ACTION in ('SetUp Start', 'Run Start','Run End')
WHERE PROCESSING_ACTION = 'Job Status'
Order by DJ_NUMBER asc, ACTION_TIME
|DJ_NUMBER|PROCESSING_ACTION|TRANSACTION_QTY|MES_MACHINE|ACTION|ACTION_TIME|
|---|---|---|---|---|---|
|19483257|Job Status|NULL|BL17|SetUp Start|2018-07-03 16:08:05.000|
|19483257|Job Status|NULL|BL17|Run Start|2018-07-03 16:08:08.000|
|19483257|Job Status|NULL|BL17|Run End|2018-07-03 16:59:05.000|
|19483258|Job Status|NULL|BL17|SetUp Start|2018-07-02 13:33:13.000|
|19483258|Job Status|NULL|BL17|Run Start|2018-07-02 13:33:17.000|
I tied to use DateDiff function but I am having conversion issue with it. My 'Run End' and 'Run Start' are string format:
DATEDIFF(Minute,'Run End','Run Start')
My goal is to get the following calculations via Pivot Operator:
Duration 1 = 'Run Start' - 'SetUp Start'
Duration 2 = 'Run End' - 'Run Start'
Final Output should be something like this:
DJ_NUMBER Duration1(Minutes) Duration2(Minutes)
19483257 0 50
19483258 0 4.00
Here is my Pivot table Operator. Here is my code:
SELECT DJ_NUMBER
, 'SetUp Start' as [SetUp]
,'Run Start' as [RunStart]
,'Run End' as [RunEnd]
-- ,DATEDIFF(Minute,'Run End','Run Start')
FROM
(
SELECT DJ_NUMBER,ACTION,ACTION_TIME
FROM [StagingDB].[dbo].[AFL_MES2O_Interface_Data_arch]
) As SourceData
PIVOT
(
COUNT( ACTION_TIME)
FOR ACTION IN ([SetUp Start], [Run Start],[Run End])
) AS PIVOTTABLE
Order by DJ_NUMBER asc
Any help to calculate the duration in a pivot table will be greatly appreciated.
Thank you..