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 7617645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:12:05+00:00 2026-05-31T03:12:05+00:00

I have a table containing user sessions, i.e. a period a user is logged

  • 0

I have a table containing user sessions, i.e. a period a user is logged in on my application. Each session has a start and end timestamp.

Can I, with one query, determine for every hour in the day how many users were online at that time (i.e. how many sessions had startTime <= hour AND endTime > hour)?
I’d like to show the averages of that for the last 2 months, but if that’s not possible I can manage with a query that does 1 day and calculate the averages in a script.

Sample result:

Hour    Online
00:00   10
01:00   12
02:00   10
....
16:00   100
17:00   120
....

(hour may also be a simple integer, doesn’t matter much)

The database is MySQL 5.

  • 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-31T03:12:06+00:00Added an answer on May 31, 2026 at 3:12 am

    I was waiting for somebody else to step in, as I am not proficient in mysql, and I believe there must be a better solution.

    Main problem here is a way to construct a table of hours in a day. Having no recursive select ability in mysql forced me to create a table by means of union. Dates are easier if we can accept missing days when nobody logged in at that date. If not, similar trick as with hours can be used to extend dates, for example for seven days.
    Cross join will produce a table of dates, each having all 24 hours of the day. Now we need to count sessions that were active at this point of time. To do that we need to truncate startTime to hour boundaries and place cross-join combined time inside truncated startTime and endTime (which does not need a truncation). Our data is finally here.

    To get average for last two months simply wrap this select in another one grouping hour and calculating avg(Users). If you really must have single query to return both datasets, you might union this query with average query, where average query would return null for date.

    Additional disclaimer: as a stated earlier I do not know MySql. I tried to write date and time conversion functions using online manual. Probably failed miserably, but I believe you will correct me. I’m also not sure about reserved keywords.

    select days.date, 
    hour,
    count (s.startTime) Users
    from
    (
        (
            select 0 hour
            union
            select 1 hour
            union
            select 2 hour
            union
            select 3 hour
            union
            select 4 hour
            union
            select 5 hour
            union
            select 6 hour
            union
            select 7 hour
            union
            select 8 hour
            union
            select 9 hour
            union
            select 10 hour
            union
            select 11 hour
            union
            select 12 hour
            union
            select 13 hour
            union
            select 14 hour
            union
            select 15 hour
            union
            select 16 hour
            union
            select 17 hour
            union
            select 18 hour
            union
            select 19 hour
            union
            select 20 hour
            union
            select 21 hour
            union
            select 22 hour
            union
            select 23 hour
        ) hours
        cross join
        (
        --  We need date portion only
            select distinct date(startTime) date from s 
            union select distinct date(endTime) from s
        ) days
    )
    left join s
    -- date+hour, hopefully
       on date_add(date, interval hour HOUR) 
       -- startTime is truncated to hour, hopefully
          between date_sub(s.startTime interval minutes(s.startTime) MINUTE) 
          and s.endTime
    -- last two months
    where days.date between date_sub (now() interval 2 MONTH) and now()
    group by days.date, hour
    order by 1, 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one table containing user sessions and another to indicate violations in the
I have a MySQL table containing userID , score . A single user can
I have a log table containing user ids, ip addresses and log in datetimes.
I have a table containing user-account permissions and I'm trying to write a query
I have a table containing roles. Many users can be assigned to a role.
I have a MySQL table containing user submitted data. I want the administrator to
I have a table which have a row containing user emails and I want
Lets say that I have a table containing users . Each row in that
Im making a simple score keeping system and each user can have stats for
I have a table containing user input which needs to be optimized. I have

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.