I have query that pulls data by week and groups it together. But it does not display weeks that doesn't have any data. I want show all weeks even if they don't have data as null maybe
Here is the query if someone can help me with this it will awesome
with calender_cte as (
select convert(date, '2020-01-01') as startdate, convert(date, '2020-02-01') as enddate
union all
select dateadd(day, 1, startdate), enddate
from calender_cte cc
where startdate < enddate
)
SELECT
min(Type) as Type,
DATEADD (week, datediff(week, 0, cc.StartDate), -1) as 'WeekOf',
DATEADD (week, datediff(week, 0, cc.StartDate), +5) as 'to',
ISNULL(DATEPART(wk, inst.StartDate), 0) as 'WeekNumber',
Sum(CASE When Type='Outage' Then (DATEDIFF(MINUTE, inst.StartDate,inst.EndDate)) Else 0 End) as 'OutageInMinutes',
Sum(CASE When Type='Degradation' Then (DATEDIFF(MINUTE, inst.StartDate,inst.EndDate)) Else 0 End) as 'DegradationInMinutes'
FROM calender_cte cc LEFT JOIN
[DESOutage].[dbo].[OPSInterruption] inst
ON CC.startdate >= inst.startdate and CC.startdate <= inst.enddate
Group by DATEADD (week, datediff(week, 0, cc.StartDate), -1),
ISNULL(DATEPART(wk, cc.StartDate), 0),
DATEADD (week, datediff(week, 0, cc.StartDate), +5)
option (maxrecursion 0);
*****************Output*****************
As you could see week 2 and 4 is missing out since there is no data being returned. I would still like to see week 2 and 4 in the output with maybe 0 as result. Also now when I update the query based on suggestions below i am not getting any data even though I am getting dates and week number