I have a table with Date, Category, and Values. It is similar like this:
Date Category Sell
01/01/21 Apple 1
01/01/21 Orange 3
01/01/21 Banana 2
02/01/21 Banana 1
03/01/21 Orange 2
What I would like to do is to create a date row for each category even on the day that nothing was sell. The desired output should have the granularity of date and category:
Date Category
01/01/21 Apple
01/01/21 Orange
01/01/21 Banana
02/01/21 Apple
02/01/21 Orange
02/01/21 Banana
03/01/21 Apple
03/01/21 Orange
03/01/21 Banana
I would like to have it in this format, but I'm not sure what is the best way to tackle this? I'm quite new to SQL so not sure what to search for. I am using Redshift SQL. My idea is to create a separate table of date and then separate table of category and then join it (but maybe there is a better approach)?
CREATE TABLE #DateIntervals (
PeriodStartDate date,
PeriodEndDate date
)
DECLARE @StartDate datetime = '01/01/2021';
DECLARE @EndDate datetime = DATEADD(DAY, -1, DATEADD(YEAR, 5, @StartDate));
WHILE @StartDate <= @EndDate
BEGIN
INSERT INTO #DateIntervals (PeriodStartDate, PeriodEndDate)
SELECT @StartDate, DATEADD(day, 6, @StartDate)
SET @StartDate = DATEADD(day, 7, @StartDate)
END
However there is an error with this code as it said "Invalid operation: syntax error at or near "DECLARE" Position"
Would be greatly appreciated if anyone could give me a suggestion.