Thursday 1 April 2010

SQL Server 2005: How to report time-series data in vanilla SQL

Let's assume I have a table recording some user session data like the following.



Now, I want to report the number of concurrent sessions at a 5 minute interval. Suppose the report time window is from 7 am - 7 pm. How to build a time series table?


The SQL above gives a time series table like: 07:00 07:05 07:10 07:15 ... 20:00

Define the report table as this



Let me populate the report table using the query as below.



I can see at what time on which day of the month the system has the max concurrent session.