WITH cl
AS (SELECT *
FROM cartopialistings
WHERE state = 'Withdrawn')
SELECT D.fulldate,
PeriodDate = CASE
WHEN d.fulldate BETWEEN ( cl.createdon - 1 ) AND
cl.modifiedon
THEN 1
ELSE 0
END,
cl.*
FROM cl
CROSS JOIN dim.date D
WHERE d.fulldate BETWEEN (SELECT Min(cl.createdon) - 1
FROM cl) AND (SELECT Max(cl.modifiedon)
FROM cl)
ORDER BY D.fulldate