Not so much a WHERE clause as a GROUP BY clause. Simply put, a query will only return data for existing rows. This means that when you group by timestamp date, only days for which there are rows will be returned. SQL Server cannot know from the context that you want to "fill in the blanks", and it would not know what s.
The usual answer is a CTE, which produces all the days you want to see, filling in the blanks. This is a bit complicated because it requires a recursive SQL statement, but this is a known trick:
WITH CTE_Dates AS ( SELECT @START AS cte_date UNION ALL SELECT DATEADD(DAY, 1, cte_date) FROM CTE_Dates WHERE DATEADD(DAY, 1, cte_date) <= @END ) SELECT cte_date as TIME_STAMP, ISNULL(COUNT(*), 0) AS counted_leads, FROM CTE_Dates LEFT JOIN HL_Logs ON DATEADD(dd, 0, DATEDIFF(dd, 0, Time_Stamp)) = cte_date WHERE Time_Stamp between @BEGIN and @END and ID_Location = @LOCATION GROUP BY cte_date
Interrupting it, the CTE uses a join that references recursively adds one day at a time before the previous date and remembers that date as part of the table. If you run a simple statement that used CTE and just selected *, you will see a list of dates between the beginning and the end. The operator then attaches this list of dates to the log table based on the date of the timestamp of the log, saving dates that have no log entries using the left join (takes all rows from the "left" side, regardless of whether they have the corresponding rows in the row "to the right) " or not). Finally, we group by date and score instead, and we should get the answer you want.
source share