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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:50:46+00:00 2026-06-07T13:50:46+00:00

I think I have a tough one here… :( I am trying to get

  • 0

I think I have a tough one here… 🙁

I am trying to get an order count by month, even when zero. Here’s the problem query:

SELECT datename(month, OrderDate) as Month, COUNT(OrderNumber) AS Orders
FROM OrderTable
WHERE OrderDate >= '2012-01-01' and OrderDate <= '2012-06-30'
GROUP BY year(OrderDate), month(OrderDate), datename(month, OrderDate)

What I’m looking to get is something like this:

Month            Orders
-----            ------
January          10
February         7
March            0
April            12
May              0
June             5

…but my query skips a row for March and May. I’ve tried COALESCE(COUNT(OrderNumber), 0) and ISNULL(COUNT(OrderNumber), 0) but I’m pretty sure the grouping is causing that not to work.

  • 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-07T13:50:47+00:00Added an answer on June 7, 2026 at 1:50 pm

    This solution doesn’t require you to hard-code the list of months you might want, all you need to do is provide any start date and any end date, and it will calculate the month boundaries for you. It includes year in the output so that it will support more than 12 months and so that your start and end dates can cross a year boundary and still order correctly and show the correct month and year.

    DECLARE @StartDate SMALLDATETIME, @EndDate SMALLDATETIME;
    
    SELECT @StartDate = '20120101', @EndDate = '20120630';
    
    ;WITH d(d) AS 
    (
      SELECT DATEADD(MONTH, n, DATEADD(MONTH, DATEDIFF(MONTH, 0, @StartDate), 0))
      FROM ( SELECT TOP (DATEDIFF(MONTH, @StartDate, @EndDate) + 1) 
        n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
        FROM sys.all_objects ORDER BY [object_id] ) AS n
    )
    SELECT 
      [Month]    = DATENAME(MONTH, d.d), 
      [Year]     = YEAR(d.d), 
      OrderCount = COUNT(o.OrderNumber) 
    FROM d LEFT OUTER JOIN dbo.OrderTable AS o
      ON o.OrderDate >= d.d
      AND o.OrderDate < DATEADD(MONTH, 1, d.d)
    GROUP BY d.d
    ORDER BY d.d;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I have got something simple wrong here. I am trying to pass
I think I have a problem with casting. $db = mysqli_connect('127.0.0.1','root','password','test'); if (! $db)
I think I have a big problem. I have a two projects solution. First
I think I have a synchronization problem...It may be too basic..Please help.. I have
I think I have a problem in understanding the proper way of using MVC.
Here's a tough one: I'm working on a website where most of the content
Here is the problem: We have some pdf files with youtube videos embedded in
So, here is my little problem. Let's say I have a list of buckets
I have a problem and I hope one of you can suggest a better
I have encountered with this problem often. Couldn't think of a way to how

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.