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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:19:30+00:00 2026-06-18T19:19:30+00:00

I’m trying to count entries grouped per hour. I’ve found some useful info inform

  • 0

I’m trying to count entries grouped per hour.
I’ve found some useful info inform on different sites and here on: MySQL Group By Hours

But the result is not what I’ve expected.
With the following code I get:

SELECT   CONCAT(Hour, ':00-', Hour+1, ':00') AS Hours,
     COUNT(`usage_time`) AS `usage` FROM `usage`
  RIGHT JOIN (
                   SELECT  0 AS Hour
         UNION ALL SELECT  1 UNION ALL SELECT  2 UNION ALL SELECT  3
         UNION ALL SELECT  4 UNION ALL SELECT  5 UNION ALL SELECT  6
         UNION ALL SELECT  7 UNION ALL SELECT  8 UNION ALL SELECT  9
         UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
         UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
         UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18
         UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21
         UNION ALL SELECT 22 UNION ALL SELECT 23
  )      AS AllHours ON HOUR(`usage_time`) = Hour

WHERE `usage_function` LIKE 'PlayedWholeSong' AND `usage_date` = DATE_SUB(CURDATE(), INTERVAL 0 DAY) OR `usage_time` IS NULL
GROUP BY Hour
ORDER BY Hour

Result:

Hours       usage
2:00-3:00   0
4:00-5:00   6
6:00-7:00   2
8:00-9:00   3
9:00-10:00  20
10:00-11:00 1
14:00-15:00 14
15:00-16:00 1
16:00-17:00 32
17:00-18:00 10

As these are entry’s from today, I don’t have any entries after 19:00.
Also I don’t see an entry from 00:00 – 01:00, 03:00 – 04:00 and several others are missing.
But I do want to show a list with every 24 hour and the result, even if there’s nothing.
String thing is the result shows a 0 between 02:00 – 03:00.
I’ve learned a lot today about mysql, but nothing that solves my issue.

I hope you can learn me something, doesn’t have to be code, a direction would be great.

  • 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-06-18T19:19:32+00:00Added an answer on June 18, 2026 at 7:19 pm

    I prefer LEFT JOIN over RIGHT JOIN personally. That way you can add your WHERE criteria in your JOIN and it won’t constrict your results. Try this:

    SELECT   CONCAT(Hour, ':00-', Hour+1, ':00') AS Hours,
         COUNT(`usage_time`) AS `usage` 
    FROM 
       (
             SELECT  0 AS Hour
             UNION ALL SELECT  1 UNION ALL SELECT  2 UNION ALL SELECT  3
             UNION ALL SELECT  4 UNION ALL SELECT  5 UNION ALL SELECT  6
             UNION ALL SELECT  7 UNION ALL SELECT  8 UNION ALL SELECT  9
             UNION ALL SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
             UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL SELECT 15
             UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18
             UNION ALL SELECT 19 UNION ALL SELECT 20 UNION ALL SELECT 21
             UNION ALL SELECT 22 UNION ALL SELECT 23
      )      AS AllHours 
          LEFT JOIN `usage` ON HOUR(`usage_time`) = Hour 
               AND `usage_function` LIKE 'PlayedWholeSong' 
               AND `usage_date` = DATE_SUB(CURDATE(), INTERVAL 0 DAY) 
    GROUP BY Hour
    ORDER BY Hour
    

    Here is a simplified SQL Fiddle.

    Good luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.