Here is my working query:
DECLARE @StartDate AS DATETIME
DECLARE @EndDate AS DATETIME
DECLARE @CurrentDate AS DATETIME
SET @StartDate = '2021-01-01'
SET @EndDate = GETDATE()
SET @CurrentDate = @StartDate
WHILE (@CurrentDate < @EndDate)
BEGIN
SELECT
SUM(CASE WHEN [CreatedDateTime] BETWEEN @CurrentDate AND @CurrentDate + 6 THEN 1 ELSE 0 END)
FROM Table
WHERE OwnedBy = 'Service Desk'
SET @CurrentDate = convert(varchar(30), dateadd(day,6, @CurrentDate), 101);
END
which produces the following result:
I'm trying to create a dynamic column name that shows the Date range for each result.
For example:
The 1st result should have the column name: "Jan 1-Jan 7"
The 2nd result should have the column name: "Jan 8-Jan 14"
etc.
Is there a way to do this?
Thanks.
