I have a table sales
product_id period_start period_end sale
----------------------------------------
1 2019-01-25 2019-02-28 100
2 2018-12-01 2020-01-01 10
3 2019-12-01 2020-01-31 1
I want an output as:
product_id period_start period_end sale
----------------------------------------
1 2019-01-25 2019-02-28 100
1 2019-01-26 2019-02-28 100
1 2019-01-27 2019-02-28 100
. . . .
. . . .
1 2019-02-28 2019-02-28 100
2 2018-12-01 2020-01-01 10
2 2018-12-02 2020-01-01 10
. . . .
. . . .
2 2020-01-01 2020-01-01 10
3 2019-12-01 2020-01-31 1
3 2019-12-02 2020-01-31 1
. . . .
. . . .
3 2020-01-31 2020-01-31 1
I tried the below but it doesn't seem to work
with alldates as(
select * from sales
union all
select product_id, dateadd(day, 1, period_start) as period_start, period_end, average_daily_sales
from alldates
where period_start < period_end
)
Any way to achieve this using recursive cte?
DDL:
create table sales (
product_id int,
period_start date,
period_end date,
average_daily_sales int
);
insert into sales values(1,'2019-01-25','2019-02-28',100),(2,'2018-12-01','2020-01-01',10),(3,'2019-12-01','2020-01-31',1);