I am working in SQL Server 2008. I am writing a stored procedure which aggregates data on a weekly basis.
Code for aggregation is
ALTER PROCEDURE [dbo].[ups_URLBatchStats]
(@startDate datetime, @endDate datetime,@source varchar(8))
AS BEGIN
SELECT
DATEADD(wk, DATEDIFF(wk, 0, ScheduleDate), 6) AS [Week Commencing],
SUM(Unresolved) AS Unresolved,
SUM(Resolved) AS Resolved,
SUM(TurkSpend) AS TurkSpend
FROM
dbo.V_URLBatchStats
WHERE
ScheduleDate BETWEEN @startDate AND @endDate
AND Source = ISNULL(@source, Source)
GROUP BY
DATEADD(wk, DATEDIFF(wk, 0, ScheduleDate), 6)
ORDER BY
DATEADD(wk, DATEDIFF(wk, 0, ScheduleDate), 6)
END
Show it gives me correct result, but if there are no data for any week I have to show it with values of ‘0’ in the columns.
So how can I handled these skipped weekly rows?
Thanks in advance.
It seems like you are currently selecting the wrong week, I tried to fix this: