I have a table that has three columns.
- When the task was supposed to start (nominalStartTime)
- When it did start (startTime)
- When it ended (endTime)
There around a bunch of tasks that are scheduled to run every 15 minutes. I need to know the difference between when the first task started and when the last task ended. For example, see below
NominalStartTime | startTime | endTime
____________________|_____________|____________
2012-09-19 08:15:00 | 08:15:41.27 | 08:15:47.00
2012-09-19 08:15:00 | 08:15:45.13 | 08:15:45.43
2012-09-19 08:15:00 | 08:15:49.88 | 08:15:50.13
2012-09-19 08:30:00 | 08:30:25.27 | 08:30:26.00
2012-09-19 08:30:00 | 08:30:45.13 | 08:30:45.43
2012-09-19 08:30:00 | 08:30:49.88 | 08:30:50.13
I’m looking for the query to return
NominalStartTime | startTime | endTime | difference
____________________|_____________|_____________|____________
2012-09-19 08:15:00 | 08:15:41.27 | 08:15:50.13 | 00:00:08.86
2012-09-19 08:30:00 | 08:30:25.27 | 08:30:50.13 | 00:00:24.86
I tried this but I’m getting aggregate group by errors and the like
Select NominalStart, MIN(TimeStarted) as TimeStarted, MAX(TimeEnded) as TimeEnded, DATEDIFF(ms, TimeStarted, TimeEnded)
FROM taskRuns
GROUP BY NominalStart
ORDER BY NominalStart
Try this solution:
SQL Fiddle Demo