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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:40:06+00:00 2026-06-05T18:40:06+00:00

Referring back to this SO post If there is a Grouping category Category which,

  • 0

Referring back to this SO post

If there is a Grouping category “Category” which, for simplicity’s sake, can be either X or Y – is it a trivial matter amending this script so that it will add in the missing dates for each of the categories ?

I assume the category will need adding into the CTE?

In other words if I have the following initial table:

enter image description here

…how do I get to the following:

enter image description here

Will upload my attempt shortly

I’ve called the initial table #x. I’m hoping to adapt a recursive CTE query like the following to include the field Category:

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)

;WITH times AS 
    ( 
    SELECT @MinDate dt , 1 depth 
    UNION ALL 
    SELECT 
            DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM times 
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES t
    LEFT OUTER JOIN #X x
        ON 
        t.dt = x.Date

Ok – I’ve tied including a CROSS JOIN but it expands things incorrectly:

SELECT DISTINCT Category INTO #Cat FROM #x

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)


;WITH times AS 
    ( 
    SELECT 
            Category
            , @MinDate dt 
            , 1 depth 
    FROM #Cat
    UNION ALL 
    SELECT 
            c.Category
            , DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM 
            times t
            CROSS JOIN #Cat c
                --ON c.Category IS NOT NULL
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES

This seems to have worked ok:

SELECT DISTINCT Category INTO #Cat FROM #x

DECLARE @MinDate DATETIME; 
SET @MinDate =  (SELECT Min(DATE) FROM #x)

DECLARE @MaxDate DATETIME; 
SET @MaxDate =  (SELECT Max(DATE) FROM #x)


;WITH times AS 
    ( 
    SELECT 
            Category
            , @MinDate dt 
            , 1 depth 
    FROM #Cat
    UNION ALL 
    SELECT 
            Category
            , DATEADD(d, depth, @MinDate) dt 
            , 1 + depth as depth 
    FROM 
            times t
    WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
    ) 
SELECT 
    * 
FROM 
    TIMES
  • 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-05T18:40:07+00:00Added an answer on June 5, 2026 at 6:40 pm

    Here is a solution without a calendar table (which is a must in production). You might have date range in variables, or you might go for min() and max() from the_table.

    EDIT: shorter version incorporating categories into date range generation

    declare @startdate datetime = '2012-1-1'
    declare @enddate datetime = '2012-1-5'
    
    ; with dates([date], category) as (
      select distinct @startdate, category
        from the_table
       union all
      select dateadd (day, 1, [date]), category
        from dates
       where [date] < @enddate
    )
    select dates.date, 
           dates.category, 
           isnull(the_table.amount, 0) Amount
      from dates
      left join the_table
        on dates.date = the_table.date
       and dates.category = the_table.category
     order by dates.category, dates.date
    option (maxrecursion 0)
    

    There is live test @ Sql Fiddle.
    New Sql Fiddle.

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

Sidebar

Related Questions

Referring to this question , how can i get the current page size in
referring to yesterday's post, this woke me up this morning. Why does this actually
referring to this question , I've decided to duplicate the tables every year, creating
Referring to the Configuration Manager under the Build menu, Is there a way to
Referring to this question: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me class Form { protected $inputs = array(); public function
Referring to this question, let's say we have the following scenario - A model
I find myself assigning this to a variable so I can easily use it
By referring to this link I try to implement this powerful tool and facing
first of all this is my first post so maybe the whole description may
I actually asked this question before, but I cannot get my account details back,

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.