I\'m trying to calculate the total lost machine time in the database. The columns in the table are MachineID aka PlantID, StartTime, EndTime.
In theory its simply sort t
This was the resulting query I went with :)
SELECT SUM(a.StartTime - a.LagEnd) as LostTime
FROM (SELECT [PlantID], [StartTime], [EndTime],
LAG([PlantID]) OVER (ORDER BY PlantID, StartTime) LagPlantID,
LAG([EndTime]) OVER (ORDER BY PlantID, StartTime) LagEnd
FROM MachineRecords) a
WHERE a.PlantID = a.LagPlantID