Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6552085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:25:02+00:00 2026-05-25T12:25:02+00:00

I have a table where we log users who have logged on to our

  • 0

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]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:25:03+00:00Added an answer on May 25, 2026 at 12:25 pm

    For SQL Server you could do…

    declare @DayToCheck datetime 
    set @DayToCheck = '2011-09-12'
    
    ;with C as
    (
      select @DayToCheck as H
      union all
      select dateadd(hour, 1, H)
      from C
      where dateadd(hour, 1, H) < dateadd(day, 1, @DayToCheck)
    )
    select C.H as [hour],
           count(L.userID) as [count of users]
    from C
      left outer join LogonTimes as L
        on L.LogoffTime > C.H and 
           L.LoginTime < dateadd(hour, 1, C.H)
    group by C.H       
    

    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.

    declare @DayToCheck datetime
    set @DayToCheck = '2011-09-12'
    
    select dateadd(hour, N.number, @DayToCheck) as [hour],
           count(L.userID) as [count of users]
    from master..spt_values as N
      left outer join LogonTimes as L
        on L.LogoffTime > dateadd(hour, N.number, @DayToCheck) and
           L.LoginTime < dateadd(hour, N.number + 1, @DayToCheck)
    where N.Type = 'P' and
          N.Number between 0 and 23       
    group by dateadd(hour, N.number, @DayToCheck)       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that functions as an event log and stores the users
I have table of users called users and log table called ulog . Every
I have a log table containing user ids, ip addresses and log in datetimes.
I have a table that contains log entries for a program I'm writing. I'm
I programmed a CMS that has a log of who has recently logged into
I have this idea which I need some help with. Currently I am developing
I have stored procedures that take a user's username (to log who makes changes
I have a table in an RDBMS which logs the user logins. A user
Assuming we have to log all the users activties of a community, i guess
Okay, simple question.. :) In my webportal I got users, who obviously need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.