I have query that returns a count of records with a matching date:
DECLARE @StartDate AS DATE = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
DECLARE @EndDate AS DATE = DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
SELECT
DISTINCT(CAST(Response_Date AS DATE)) AS Response_Date
,COUNT(CAST(Response_Date AS DATE)) AS Response_Count
...
WHERE Response_Date BETWEEN @StartDate AND @EndDate
And I get a return as expected.
^Response_Date^Response_Count^
|2016-09-22 |2 |
|2016-09-24 |4 |
...
However, these values will be used in report builder in lists and charts. The problem is that there are gaps between the dates. Is there some way to run the query returning all dates from previous months, and a 0 when nothing is on that date?
Here is my current query:
DECLARE @JurisdictionName AS CHAR(30) = 'Something'
DECLARE @StartDate AS DATE = DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
DECLARE @EndDate AS DATE = DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
SELECT
DISTINCT(CAST(Response_Master_Incident.Response_Date AS DATE)) AS Response_Date
,COUNT(CAST(Response_Master_Incident.Response_Date AS DATE)) AS Response_Count
FROM
Response_Master_Incident
WHERE EXISTS
(SELECT *
FROM Activity_Log
WHERE
Activity_Log.Radio_Code IS NOT NULL
AND (Radio_Name LIKE 'V[0-9][0-9]' OR Radio_Name LIKE 'U[0-9][0-9]')
AND Response_Master_Incident.ID = Activity_Log.Master_Incident_ID
)
AND Response_Master_Incident.Time_First_Unit_Assigned IS NOT NULL
AND Response_Master_Incident.Response_Date BETWEEN @StartDate AND @EndDate
GROUP BY CAST(Response_Master_Incident.Response_Date AS DATE)