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

  • Home
  • SEARCH
  • 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 976033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:40:55+00:00 2026-05-16T03:40:55+00:00

hope you can help me. I’ve a DB table, lets call it activities [activities]

  • 0

hope you can help me.

I’ve a DB table, lets call it activities

[activities]
 [start_time - datetime]
 [end_time - datetime]
 [name - string]

i need a query which will return me, for each hour of a 24day how many activites were active at that time.

for example:

[time]         [usage]
11:00 (11 am)    12

etc…

  • 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-16T03:40:55+00:00Added an answer on May 16, 2026 at 3:40 am

    You may want to use create another table (could be temporary) that holds the numbers 0-23 representing the hours of the day:

    CREATE TABLE hours_of_day (hour int);
    INSERT INTO hours_of_day VALUES (1), (2), (3), (4), (5), (6), (7), (8),
                                    (9), (10), (11), (12), (13), (14), (15), 
                                    (16), (17), (18), (19), (20), (21), (22), (23);
    

    Then you should be able to use a query such as the following (using MySQL):

    SELECT      hd.hour, COUNT(a.name) `usage`
    FROM        hours_of_day hd
    LEFT JOIN   activities a ON 
                (hd.hour BETWEEN HOUR(a.start_time) AND HOUR(a.end_time))
    GROUP BY    hd.hour;
    

    Test case:

    CREATE TABLE activities (start_time datetime, 
                             end_time   datetime, 
                             name       varchar(10));
    
    INSERT INTO activities VALUES 
         ('2010-01-01 12:10:00', '2010-01-01 13:10:00', 'b'),
         ('2010-01-01 13:20:00', '2010-01-01 13:30:00', 'c'),
         ('2010-01-01 13:50:00', '2010-01-01 14:05:00', 'd'),
         ('2010-01-01 17:20:00', '2010-01-01 20:30:00', 'e');
    

    Result:

    +------+-------+
    | hour | usage |
    +------+-------+
    |    1 |     0 |
    |    2 |     0 |
    |    3 |     0 |
    |    4 |     0 |
    |    5 |     0 |
    |    6 |     0 |
    |    7 |     0 |
    |    8 |     0 |
    |    9 |     0 |
    |   10 |     0 |
    |   11 |     0 |
    |   12 |     1 |
    |   13 |     3 |
    |   14 |     1 |
    |   15 |     0 |
    |   16 |     0 |
    |   17 |     1 |
    |   18 |     1 |
    |   19 |     1 |
    |   20 |     1 |
    |   21 |     0 |
    |   22 |     0 |
    |   23 |     0 |
    +------+-------+
    23 rows in set (0.00 sec)
    

    If you don’t want all those 0s, you can use INNER JOIN instead of LEFT JOIN. The results we be as follows:

    +------+-------+
    | hour | usage |
    +------+-------+
    |   12 |     1 |
    |   13 |     3 |
    |   14 |     1 |
    |   17 |     1 |
    |   18 |     1 |
    |   19 |     1 |
    |   20 |     1 |
    +------+-------+
    7 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question really has three related parts which I hope you can help me
I hope there's a SharePoint expert here on SO who can help with this.
I hope someone can guide me as I'm stuck... I need to write an
Hope someone can help. I have the code below which when the user hovers
hope you can help me solving my problem. These two images are linked somewhere
I hope anyone can help. I have to develop up against this third party
I hope you can help me here... I have a conflict with the decimal
I hope you can help solving my problems. Any help is much appreciated. Thank
I hope you can help with that. OK i have created new group/folder under
i hope you can help me with this one. The code is supposed 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.