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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:35:25+00:00 2026-06-04T03:35:25+00:00

I want to fill in the missing months in a SELECT statement so I

  • 0

I want to fill in the missing months in a SELECT statement so I was planning on joining my table with another table that contains all months. How can I generate a table of months in a light-weight fashion? For instance,

CREATE TABLE #TEMP(Timewhen DATETIME, Value INT)

INSERT INTO #TEMP VALUES('2012-02-04', 4)
INSERT INTO #TEMP VALUES('2012-02-06', 4)
INSERT INTO #TEMP VALUES('2012-02-10', 4)
INSERT INTO #TEMP VALUES('2012-04-08', 4)
INSERT INTO #TEMP VALUES('2012-04-12', 4)

SELECT YEAR(Timewhen) EventYear, MONTH(Timewhen) EventMonth, SUM(Value) Total
FROM #TEMP
GROUP BY YEAR(Timewhen), MONTH(Timewhen)

DROP TABLE #TEMP

gives me:

EventYear   EventMonth  Total
2012               2    12
2012               4    8

But I need:

EventYear   EventMonth  Total
2012        2           12
2012        3           0
2012        4           8
  • 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-04T03:35:27+00:00Added an answer on June 4, 2026 at 3:35 am
    DECLARE @months     INT, 
            @firstmonth DATE;
    
    SELECT 
        @months     = DATEDIFF(MONTH, MIN(Timewhen), MAX(Timewhen)) + 1, 
        @firstmonth = DATEADD(DAY, 1-DAY(MIN(Timewhen)), MIN(Timewhen))
    FROM #temp;
    
    ;WITH m(rn) AS 
    (
      SELECT TOP (@months) rn = ROW_NUMBER() OVER (ORDER BY [object_id])
      FROM sys.objects ORDER BY rn
    ),
    x(d) AS 
    (
      SELECT DATEADD(MONTH, rn-1, @firstmonth) FROM m
    )
    SELECT YEAR(x.d), MONTH(x.d), Total = SUM(COALESCE(t.Value, 0))
      FROM x 
      LEFT OUTER JOIN #temp AS t
      ON t.Timewhen >= x.d AND t.Timewhen < DATEADD(MONTH, 1, x.d)
      GROUP BY YEAR(x.d), MONTH(x.d);
    

    Or a slightly less verbose version:

    DECLARE @months     INT, 
            @firstmonth DATE;
    
    SELECT 
        @months     = DATEDIFF(MONTH, MIN(Timewhen), MAX(Timewhen)) + 1, 
        @firstmonth = DATEADD(DAY, 1-DAY(MIN(Timewhen)), MIN(Timewhen))
    FROM #temp;
    
    ;WITH x(y, m, s, e) AS 
    (
      SELECT YEAR(dt), MONTH(dt), dt, DATEADD(MONTH, 1, dt) FROM 
      ( SELECT dt = DATEADD(MONTH, rn-1, @firstmonth) FROM 
        ( SELECT TOP (@months) rn = ROW_NUMBER() OVER (ORDER BY [object_id])
          FROM sys.objects ORDER BY rn
        ) AS z
      ) AS y
    )
    SELECT EventYear = x.y, EventMonth = x.m, Total = SUM(COALESCE(t.Value, 0))
      FROM x LEFT OUTER JOIN #temp AS t
      ON t.Timewhen >= x.s AND t.Timewhen < x.e
      GROUP BY x.y, x.m;
    

    Alternate solution using Calendar table:

    Create a Calendar table using instructions from here.

    -- Script to create a calendar table
    
    DROP TABLE dbo.Numbers
    DROP TABLE dbo.Calendar
    GO
    
    -- Use this to determine the number in the next query
    DECLARE @NUMDAYS int
    SELECT @NUMDAYS = DATEDIFF(DAY, '20000101', '20291231')
    
    CREATE TABLE dbo.Numbers 
    ( 
        Number INT IDENTITY(1,1) PRIMARY KEY CLUSTERED 
    ) 
    
    WHILE COALESCE(SCOPE_IDENTITY(), 0) <= @NUMDAYS
    BEGIN 
        INSERT dbo.Numbers DEFAULT VALUES 
    END
    GO
    
    CREATE TABLE dbo.Calendar  
    (  
        dt SMALLDATETIME NOT NULL 
            PRIMARY KEY CLUSTERED,  
    
        isWeekday BIT, 
        isHoliday BIT,  
        Y SMALLINT,  
        FY SMALLINT,  
        Q TINYINT,  
        M TINYINT,  
        D TINYINT,  
        DW TINYINT, 
        monthname VARCHAR(9), 
        dayname VARCHAR(9), 
        W TINYINT ,
        HolidayDescription VARCHAR(32)
    ) 
    GO
    
    
    
    INSERT Calendar(dt) 
    SELECT DATEADD(DAY, Number, '20000101') 
    FROM dbo.Numbers 
    --WHERE Number <= @NUMDAYS 
    ORDER BY Number
    
    GO
    
    --SELECT * FROM Calendar
    
    UPDATE dbo.Calendar SET 
    
        isWeekday = CASE  
            WHEN DATEPART(DW, dt) IN (1,7)  
            THEN 0 
            ELSE 1 END, 
    
        isHoliday = 0, 
    
        Y = YEAR(dt),  
    
        FY = YEAR(dt), 
    
        /* 
        -- if our fiscal year 
        -- starts on May 1st: 
    
        FY = CASE  
            WHEN MONTH(dt) < 5 
            THEN YEAR(dt)-1  
            ELSE YEAR(dt) END, 
        */ 
    
        Q = CASE 
            WHEN MONTH(dt) <= 3 THEN 1 
            WHEN MONTH(dt) <= 6 THEN 2 
            WHEN MONTH(dt) <= 9 THEN 3 
            ELSE 4 END,  
    
        M = MONTH(dt),  
    
        D = DAY(dt),  
    
        DW = DATEPART(DW, dt),  
    
        monthname = DATENAME(MONTH, dt),  
    
        dayname = DATENAME(DW, dt),  
    
        W = DATEPART(WK, dt)
    
    GO
    

    After creating a Calendar table, one can use the following to achieve this:

    CREATE TABLE #TEMP(Timewhen DATETIME, Value INT)
    
    INSERT INTO #TEMP VALUES('2012-02-04', 4)
    INSERT INTO #TEMP VALUES('2012-02-06', 4)
    INSERT INTO #TEMP VALUES('2012-02-10', 4)
    INSERT INTO #TEMP VALUES('2012-04-08', 4)
    INSERT INTO #TEMP VALUES('2012-04-12', 4)
    
    SELECT Y EventYear, M EventMonth, SUM(Value) Total
    FROM #TEMP RIGHT OUTER JOIN (SELECT DISTINCT Y,M FROM dbo.dateRange('20120204', '20120412')) X
    ON YEAR(Timewhen) = X.Y AND MONTH(Timewhen) = X.M
    GROUP BY Y,M
    
    DROP TABLE #TEMP
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fill an array in javascript that takes all the values out
I want to fill a ListView without an array of all the same things.
I want to fill all cells with the same control. What I have now
I have a for loop. Inside that loop I want to fill up an
I have a unordered list that I want to make fill 2 columns, I
What am I missing here? I want to do a simple call to Select()
Here's what I want to achieve, I have a select box that loads in
I want to fill jqGrid from a stored procedure, there was 1 good example
I simply want to fill-up cells in my spreadsheet from a VBA function. By
I Had Drop down list and I want to fill it with data from

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.