I have to following table in sql server:
date | status
2009-01-01 12:00:00 OK
2009-01-01 12:03:00 FAILED
2009-01-01 12:04:00 OK
2009-01-01 12:06:20 OK
2009-01-01 12:07:35 FAILED
2009-01-01 12:07:40 FAILED
2009-01-01 12:20:40 FAILED
2009-01-01 12:25:40 OK
I need the following: starting 2009-01-01 12:00:00, every 10 minute from this date i need to see the number of OK and FAILED.
something like:
INTERVAL FAILED OK
2009-01-01 12:00:00-2009-01-01 12:15:00 1 2
2009-01-01 12:15:01-2009-01-01 12:30:00 0 1
etc..
what is the best way to do this in sql?
Ok first of all ..
You mention 10 minutes and the provide an example with 15 minutes.. Additionally you sample data should return different results than what you posted..
solution using Pivot
this will return
Update after comments
This version will include time intervals that do not have values in the database..
We will need to create a temporary table on the fly ..
Using a 10 minute interval now, to show a period without values, returns..