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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:39:00+00:00 2026-05-22T18:39:00+00:00

In my sql query, I count the number of orders in each Hour of

  • 0

In my sql query, I count the number of orders in each Hour of day. My query looks something like this:

SELECT   COUNT(dbo.Uputa.ID),{ fn HOUR(dbo.Orders.Date) } AS Hour
FROM      Orders       
WHERE dbo.Orders.Date BETWEEN '2011-05-01' AND '2011-05-26' 
GROUP BY { fn HOUR(dbo.Orders.Date) }
ORDER BY Hour

My problem is that the query returns only existing Hours in dbo.Orders.Date.
For example:

Number   Hour
12         3
12         5

I want to return all hours like this:

 Number      Hour
    0          0
    0          1
    0          2
    12         3
    0          4
    12         5
   ...
    0          23

Does anybody have idea how to accomplish this?

  • 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-22T18:39:01+00:00Added an answer on May 22, 2026 at 6:39 pm

    Use a common table expression to create all hours, then left join your grouped totals to get a result.

    with mycte as
    (
        SELECT 0 AS MyHour
        UNION ALL
        SELECT MyHour + 1
        FROM mycte 
        WHERE MyHour + 1 < 24
    )
    SELECT mycte.MyHour, COALESCE(OrderCount,0) FROM mycte
    LEFT JOIN 
    (
        SELECT  COUNT(dbo.Uputa.ID) AS OrderCount,{ fn HOUR(dbo.Orders.Date) } AS MyHour
        FROM    Orders       
        WHERE   dbo.Orders.Date BETWEEN '2011-05-01' AND '2011-05-26' 
        GROUP BY { fn HOUR(dbo.Orders.Date) }
    ) h
    ON
      h.MyHour = mycte.MyHour;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an sql query like this: SELECT IF( (SELECT COUNT(*) FROM `test_table2` WHERE
i have a sql query where i count the number of rows SELECT count(*)
I have a simple SQL query: Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as
I am using following SQL Query: SELECT `comment`.`id` AS `comment_id` , count( `comment_likes`.`comment_id` )
I have an SQL query with a nested join: SELECT rh.host, rh.report, COUNT(results.id), COUNT(results_2.id),
I'm looking for help using sum() in my SQL query: SELECT links.id, count(DISTINCT stats.id)
So I've got a SQL query I'd like to duplicate in rails: select g.*
I'm trying to query my SQL database to get the number of orders made
I have folowing sql query select sta_name Station,count(factory_name) as factories from gps1 group by
I'm having difficulty creating a month->count select query in SQL. Basically, I have a

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.