Let’s say my table looks like this:
Sessions
start_dts (datetime)
end_dts (datetime)
and the data looks like this:
start_dts end_dts
12/25/2011 01:55:00 12/25/2011 03:30:00
I need the query results to look like this:
Date Hour MinutesOnline
12/25/2011 0 0
12/25/2011 1 5
12/25/2011 2 60
12/25/2011 3 30
... (every hour of the date range being queried)
Is this even possible with a single query?
Here is a pretty good start. This will work for any date/time range. However, it has one main prerequisite: You need to create an
intervalstable with adt_hr datetimefield which contains all the intervals you are scanning over.–
To generate the
intervalstable, you could create a stored procedure which creates a temporary table with a date_hour sequence. See Get a list of dates between two dates for a way to do this.