I have a table where we log users who have logged on to our system. It logs when they log on and when they log off. I need to produce some SQL which gives us a list of hours of the day and the number of people logged on in that time. I can do this for one time, but I do not know how to extend this to produce each hour of the day. I don’t really want to have to do 24 SQL statements if possible!?
SELECT COUNT(userID) AS "count of users"
FROM LogonTimes
WHERE (LoginTime > '2011-09-12 09:00:00') AND (LogoffTime < '2011-09-12 10:00:00 ')
The above would produce say “3”. This shows 3 people were logged on between 9 and 10am.
Any ideas?!
[edit]It’s SQL Server as people have guessed below – sorry for not specifying! I’ll try out the suggestions and post back shortly! thanks 🙂 [/edit]
For SQL Server you could do…
Try here: https://data.stackexchange.com/stackoverflow/q/112462/
A version for SQL Server 2000 that uses a number table instead if a CTE. Here I use master..spt_values.