I have a table in which rows have dates as monday dates of the weeks. Some consecutive rows may not have consecutive weekdate and thus has gaps in between. This image will clear the situation:
As clear from the image, there is a gap between weekdates 2016-08-08 and 2016-09-05 as rows with weekdates '2016-08-15','2016-08-22','2016-08-29' are not there before '2016-09-05'.
So, how can I fill this gap with rows for all these dates and null for rest two columns?
Try this:
In the above query it will take the current date i.e
2016-09-08 15:19:06.950
and add 1 week to it and give the resultant date i.e2016-09-15 15:19:40.657
Use a tally table
CTE
.You might try this code, which will generate a list of Mondays
You can specify the count of generated rows with
@start
and@end
, the@step
should be 7 in your case. This will add 0, 7, 14, 21, ... to a given date (which should be a Monday in your case).Now use a
LEFT JOIN
to combine this with your table data. This should result in a gap-less list of all Mondays together with values - if there are any...