You have 2 questions that you are trying to solve. The first question is how to fill in the blanks. The second problem is filling in the Count field for those missing entries.
Problem 1: this can be resolved either using the Dates Lookup table , or by creating a recursive common table expression . I would recommend creating a date lookup table for this if this is an option. If you cannot create such a table, you will need something like this.
WITH CTE AS ( SELECT MAX(dt) maxdate, MIN(dt) mindate FROM yourtable ), RecursiveCTE AS ( SELECT mindate dtfield FROM CTE UNION ALL SELECT DATEADD(day, 1, dtfield) FROM RecursiveCTE R JOIN CTE T ON R.dtfield < T.maxdate )
This should create a list of dates starting with the MIN date in your table and ending with MAX .
Problem 2: this is where the correlated subquery in handy (as far as I usually avoid them) to get the last cnt from your source table:
SELECT r.dtfield, (SELECT TOP 1 cnt FROM yourtable WHERE dt <= r.dtfield ORDER BY dt DESC) cnt FROM RecursiveCTE r
source share