I have a table called ITIncidents
tracking the instances and length of time of incidents.
By getting the maximum EndDate
and comparing to GetDate()
I can get the length of time since the last incident ("It has been 3 days, 6 hours, 56 seconds since the last incident").
I would like to work out the previous 'record' of the amount of time between the end of one incident and the start of another.
Example schema and data at: http://sqlfiddle.com/#!6/6ec2a/1
I have looked at this question and tried to fit this code:
WITH rows AS
(
SELECT *, ROW_NUMBER() OVER (ORDER BY DataDate) AS rn
FROM mytable
)
SELECT DATEDIFF(second, mc.DataDate, mp.DataDate)
FROM rows mc
JOIN rows mp
ON mc.rn = mp.rn - 1
to the situation, but because I am calculating the difference between the end date on one row and the start date on another, I am struggling to even use DATEDIFF
.
In the SQLFiddle example the date of the last entry's EndDate
is 2017-11-01 00:10:00
, so as of Nov 8th 2017 00:00:00, it has been 6 days, 23 hours, 50 minutes since the last incident, but the previous 'record' was the length of time between the incident that ended on the 2017-10-09 15:10:00
and the incident that started 2017-11-01 00:00:00