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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:50:24+00:00 2026-06-01T11:50:24+00:00

I would like to create a bar chart displaying the number of objects that

  • 0

I would like to create a bar chart displaying the number of objects that were a available on a monthly base. All rows have a start and end date. I know how to do the count for a single month:

SELECT COUNT(*) As NumberOfItems
FROM Items
WHERE DATEPART(MONTH, Items.StartDate) <= @monthNumber 
AND DATEPART(MONTH, Items.EndDate) >= @monthNumber

Now I would like do create the SQL to get the month number and the number of items using a single SELECT statement.

Is there any elegant way of accomplishing this? I am aware I have to take the year number into account.

  • 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-01T11:50:26+00:00Added an answer on June 1, 2026 at 11:50 am

    Assuming Sql Server 2005 or newer.

    CTE part will return month numbers spanning years between @startDate and @endDate. Main body joins month numbers with items performing the same conversion on Items.StartDate and Items.EndDate.

    ; with months (month) as (
      select datediff (m, 0, @startDate)
      union all
      select month + 1
        from months
      where month < datediff (m, 0, @endDate)
    )
    select year (Items.StartDate) Year,
           month (Items.StartDate) Month,
           count (*) NumberOfItems
      from months
      inner join Items
         on datediff (m, 0, Items.StartDate) <= months.month
        and datediff (m, 0, Items.EndDate) >= months.month
      group by 
           year (Items.StartDate),
           month (Items.StartDate)
    

    Note: if you intend to span more than hundred months you will need option (maxrecursion 0) at the end of query.

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

Sidebar

Related Questions

I would like create my own collection that has all the attributes of python
I would like to create a horizontal proportional stacked bar chart, using R. My
I would like to create a search bar on the page that looks similar
I would like to create a stacked bar chart based on data from a
I would like to create a div that will have the screen width (any
I would like to be able to create bar charts with JFreeChart that looks
I need to create a d3 bar chart that can have negative values. Ideally
I would like to dynamically create a minimal transparent bar graph to display over
I would like create a web service in ASP.Net 2.0 that will supports JSON.
i would like create a array of structure which have a dynamic array :

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.