Referring back to this SO post
If there is a Grouping category "Category" which, for simplicity's sake, can be either X or Y - is it a trivial matter amending this script so that it will add in the missing dates for each of the categories ?
I assume the category will need adding into the CTE?
In other words if I have the following initial table:
...how do I get to the following:
Will upload my attempt shortly
I've called the initial table #x. I'm hoping to adapt a recursive CTE query like the following to include the field Category:
DECLARE @MinDate DATETIME;
SET @MinDate = (SELECT Min(DATE) FROM #x)
DECLARE @MaxDate DATETIME;
SET @MaxDate = (SELECT Max(DATE) FROM #x)
;WITH times AS
(
SELECT @MinDate dt , 1 depth
UNION ALL
SELECT
DATEADD(d, depth, @MinDate) dt
, 1 + depth as depth
FROM times
WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
)
SELECT
*
FROM
TIMES t
LEFT OUTER JOIN #X x
ON
t.dt = x.Date
Ok - I've tied including a CROSS JOIN but it expands things incorrectly:
SELECT DISTINCT Category INTO #Cat FROM #x
DECLARE @MinDate DATETIME;
SET @MinDate = (SELECT Min(DATE) FROM #x)
DECLARE @MaxDate DATETIME;
SET @MaxDate = (SELECT Max(DATE) FROM #x)
;WITH times AS
(
SELECT
Category
, @MinDate dt
, 1 depth
FROM #Cat
UNION ALL
SELECT
c.Category
, DATEADD(d, depth, @MinDate) dt
, 1 + depth as depth
FROM
times t
CROSS JOIN #Cat c
--ON c.Category IS NOT NULL
WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
)
SELECT
*
FROM
TIMES
This seems to have worked ok:
SELECT DISTINCT Category INTO #Cat FROM #x
DECLARE @MinDate DATETIME;
SET @MinDate = (SELECT Min(DATE) FROM #x)
DECLARE @MaxDate DATETIME;
SET @MaxDate = (SELECT Max(DATE) FROM #x)
;WITH times AS
(
SELECT
Category
, @MinDate dt
, 1 depth
FROM #Cat
UNION ALL
SELECT
Category
, DATEADD(d, depth, @MinDate) dt
, 1 + depth as depth
FROM
times t
WHERE DATEADD(d, depth, @MinDate) <= @MaxDate
)
SELECT
*
FROM
TIMES