I've got a table with CustomerID
, StartDate
and EndDate
.
I'm trying to create a table with the following columns: Date
, ActiveUsers
.
The Date
needs to be all dates between 01/01/2016 and today. ActiveUsers
is a count of CustomerID
where the Date
falls between the StartDate
and EndDate
.
I hope all that makes sense.
I found code that gives me a list of dates but I have no idea how I can join my customers table to this result.
DECLARE @StartDateTime DATE
DECLARE @EndDateTime DATE
SET @StartDateTime = '2016-01-01'
SET @EndDateTime = GETDATE();
WITH DateRange(DateData) AS
(
SELECT @StartDateTime as Date
UNION ALL
SELECT DATEADD(d,1,DateData)
FROM DateRange
WHERE DateData <= @EndDateTime
)
SELECT dr.DateData
FROM DateRange dr
OPTION (MAXRECURSION 0)
GO