Skip to content
Advertisement

Expanding Date Ranges

I want to expand the date ranges in the below “Original Table”. The end result should be the “Resultant Table”. I know that this can be done in sql using dateadd and ctes. However, I cannot get the join right to get the result as shown below. Please suggest possible solutions.

enter image description here

The query I am using is something like this:

The result it gives me is the original table. I have played around with various options and it gives me other incorrect results.

Advertisement

Answer

The immediate problem with your query is that you should be using a left join rather than an inner join in the outer query, so “missing” dates are not filtered out.

However, this is probably not sufficient. You also need to keep track of the original values so you can put them in the newly generated rows.

It might be simpler to generate the rows directly from the recursive query:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement