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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:15:41+00:00 2026-06-12T14:15:41+00:00

I need some help one getting the project count per month for all of

  • 0

I need some help one getting the project count per month for all of the projectTypes per region. I’ve tried the following statement but it didn’t return what I need.
The problem with the statement below is that it only returns the projectType only if there a count for that month. I need to return zero if the month doesn’t have a count.
Any help is much appreciated.

SELECT r.region, pt.projectType, count(p.id) as totalCount, p.postedOn as monthCount
FROM region r cross join ProjectTypes pt left join projects p on p.regionID = r.id and pt.id = p.TypeID 
WHERE year(p.postedOn) = '2012' 
group by r.region, pt.projectType, p.postedOn 
order by r.region

Please the sample here: http://sqlfiddle.com/#!3/6680f/18

regions:
-------------------------
id  | region    |
-------------------------
1   | East      |
-------------------------
2   | MidWest   |
-------------------------
3   | West      |
-------------------------


Project Type:
-------------------------
id  | projectType   | 
-------------------------
1   | Web Desgin    |
-------------------------
2   | Database  |
-------------------------
3   | Development   |
-------------------------


Projects:
-------------------------------------------------------------------------
id  | projectName   | regionID  | projectTypeID | postedOn  |
-------------------------------------------------------------------------
1   | Project 1 | 1     | 2     | 2012-09-02    |
-------------------------------------------------------------------------
2   | Project 2 | 2     | 2     | 2012-09-02    |
-------------------------------------------------------------------------
3   | Project 3 | 1     | 1     | 2012-09-02    |
-------------------------------------------------------------------------
4   | Project 4 | 3     | 2     | 2012-09-02    |
-------------------------------------------------------------------------
5   | Project 5 | 3     | 1     | 2012-10-02    |
-------------------------------------------------------------------------
6   | Project 6 | 3     | 2     | 2012-10-02    |
-------------------------------------------------------------------------
7   | Project 7 | 3     | 3     | 2012-10-02    |
-------------------------------------------------------------------------
8   | Project 8 | 2     | 3     | 2012-10-02    |
-------------------------------------------------------------------------
9   | Project 9 | 1     | 2     | 2012-10-02    |
-------------------------------------------------------------------------
10  | Project 10    | 1     | 2     | 2012-10-02    |
-------------------------------------------------------------------------


Desired Results:
---------------------------------------------------------
Region  | project Type  | totalCount    | monthCount    |
---------------------------------------------------------
East    | Web Desgin    | 1     | September |
---------------------------------------------------------
East    | Database  | 1     | September |
---------------------------------------------------------
East    | Development   | 0     | September |
---------------------------------------------------------
Midwest | Web Desgin    | 0     | September |
---------------------------------------------------------
Midwest | Database  | 1     | September |
---------------------------------------------------------
Midwest | Development   | 0     | September |
---------------------------------------------------------
West    | Web Desgin    | 0     | September |
---------------------------------------------------------
West    | Database  | 1     | September |
---------------------------------------------------------
West    | Development   | 0     | September |
---------------------------------------------------------
East    | Web Desgin    | 0     | October   |
---------------------------------------------------------
East    | Database  | 2     | October   |
---------------------------------------------------------
East    | Development   | 0     | October   |
---------------------------------------------------------
Midwest | Web Desgin    | 0     | October   |
---------------------------------------------------------
Midwest | Database  | 0     | October   |
---------------------------------------------------------
Midwest | Development   | 1     | October   |
---------------------------------------------------------
West    | Web Desgin    | 1     | October   |
---------------------------------------------------------
West    | Database  | 1     | October   |
---------------------------------------------------------
West    | Development   | 1     | October   |
---------------------------------------------------------
  • 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-12T14:15:43+00:00Added an answer on June 12, 2026 at 2:15 pm

    The WITH block defines a virtual table of months (or rather, the first days of each month) required. This extends your CROSS JOIN to give you all the months.

    ;WITH months(startdate) AS (
         SELECT CAST('20120901' AS date)
      UNION ALL
         SELECT dateadd(m,1,startdate)
           FROM months
          WHERE startdate < '20121001'
    )
    
        SELECT r.region,
               pt.projectType,
               count(p.id) totalCount,
               DATENAME(Month,m.startdate) monthCount
          FROM region r
    CROSS JOIN ProjectTypes pt
    CROSS JOIN months m
     LEFT JOIN projects p ON p.regionID = r.id
           AND pt.id = p.TypeID
           AND p.postedOn >= m.startdate
           AND p.postedOn <  dateadd(m,1,m.startdate)
      GROUP BY r.region, pt.projectType, m.startdate
      ORDER BY m.startdate, region, projecttype
        OPTION (maxrecursion 0);
    

    I applied this to your fiddle

    For SQL Server 2000, or for performance really, create the table Months as a proper table and fill it with months from 1999 through the year 2169, e.g.

    CREATE TABLE Months (
      startdate datetime -- the first day of month
        primary key
    );
    insert Months
    select DateAdd(M,Number,'19990101')
    from master..spt_values
    where type='P'
    GO
    

    Then, just choose the month range you need using the condition against the months table, i.e. as below:

        SELECT r.region,
               pt.projectType,
               count(p.id) totalCount,
               DATENAME(Month,m.startdate) monthCount
          FROM region r
    CROSS JOIN ProjectTypes pt
          JOIN months m on m.startdate between '20120901' and '20121001'
     LEFT JOIN projects p ON p.regionID = r.id
           AND pt.id = p.TypeID
           AND p.postedOn >= m.startdate
           AND p.postedOn <  dateadd(m,1,m.startdate)
      GROUP BY r.region, pt.projectType, m.startdate
      ORDER BY m.startdate, region, projecttype;
    

    Updated SQL Fiddle

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

Sidebar

Related Questions

We Really need some help on this one: We've Struggled with all the Apple
Hello I need some help getting my mysql correct. I need to select one
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
I need some help dealing with three Threads in Android One thread is the
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
I need some help getting data from several tables. This is the tables I
I'm new to working with arrays so I need some help. With getting just
I need some help getting a SQL query running the way I need. I
I need some help getting my head around thread safety and dictionaries. When adding
I need some help with a regex conundrum pls. I'm still getting to grips

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.