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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:13:34+00:00 2026-06-14T07:13:34+00:00

I have a pretty simple table here: Table1 ID Date1 Date2 Date3 I want

  • 0

I have a pretty simple table here:


Table1
ID
Date1
Date2
Date3


I want to summarize the data by month and ID so the query result comes out like this:


Query Result of table1
ID
Month
Count of Date1 from the month that’s not null
Count of Date2 from the month that’s not null
Count of Date3 from the month that’s not null


I currently have this query below that only pulls one month depending on what @month is specified to:

Select ID, @month as [Month]  
Sum((Case When Month(Date1)=@month then 1 else 0 END)) as [Date1 Count],   
Sum((Case When Month(Date2)=@month then 1 else 0 END)) as [Date2 Count],  
Sum((Case When Month(Date3)=@month then 1 else 0 END)) as [Date3 Count],  
From Table1  
Group by ID 

But I want to pull all 12 month, however the database does not have a month table or calendar table, how can I create a query that will include all 12 month for each IDs and get their corresponding monthly counts for Date1, Date2, and Date3? Btw, a lot of the entries have one of the Dates empty, e.g. Date1 and Date3 = Null whereas Date2 = ’01/01/2008′. Thanks in advance!

  • 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-14T07:13:36+00:00Added an answer on June 14, 2026 at 7:13 am

    Since there are only 12 months and that will never change, it’s simple enough to use a CTE or subquery to generate the month numbers:

    ;with M as (
      select 1 as month union
      select 2 union
      select 3 union
      select 4 union
      select 5 union
      select 6 union
      select 7 union
      select 8 union
      select 9 union
      select 10 union
      select 11 union
      select 12
    )
    Select ID, M.Month,
    Sum((Case When Month(Date1)=M.Month then 1 else 0 END)) as [Date1 Count],   
    Sum((Case When Month(Date2)=M.Month then 1 else 0 END)) as [Date2 Count],  
    Sum((Case When Month(Date3)=M.Month then 1 else 0 END)) as [Date3 Count]  
    From M,Table1
    Group by ID,M.month
    

    Demo: http://www.sqlfiddle.com/#!3/19ed6b/1

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

Sidebar

Related Questions

I have a pretty simple Linq to XML query: var q = from c
I have a hopefully pretty simple question here. I'm converting some Access SQL script
I want to achieve a pretty simple task, but none of the solutions here
I have a pretty simple set of data requirements to do with pets and
In my app I have a table for ROLES. Pretty simple: 1: Admin, DESC
I have pretty simple jquery code : $(document).ready(function(){ $('img.marqFl').on({ mouseenter: function() { $(this).animate({height: 300},
i have pretty simple simple question (i hope so). How do i change the
I have a pretty simple trigger: CREATE OR REPLACE FUNCTION f_log_datei() RETURNS TRIGGER AS
I have a pretty simple ASP.NET Web Form that looks a bit like the
I have a pretty simple SQL I need to perform. I have a ProcessUser

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.