By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE Parent (ID INTEGER, StartYear VARCHAR(4), EndYear VARCHAR(4), Capacity FLOAT);
INSERT INTO Parent (ID, StartYear, EndYear, Capacity) VALUES
('1', '2016', '2020', '2.0'),
('2', '2018', '2021', '3.5');
2 rows affected
CREATE TABLE Child (ID INTEGER, Year VARCHAR(4), Capacity FLOAT);
declare @maxendyear int = cast((select max(endyear) from parent) as int);
with years AS (
select cast(min(startyear) as int) year from parent
union all
select year + 1 FROM years where year + 1 <= @maxendyear
)
insert into child (id, year, capacity)
select
p.id, y.year, p.capacity
from parent p inner join years y
on y.year between p.startyear and p.endyear
9 rows affected
select * from child
order by id, year
ID | Year | Capacity |
---|---|---|
1 | 2016 | 2 |
1 | 2017 | 2 |
1 | 2018 | 2 |
1 | 2019 | 2 |
1 | 2020 | 2 |
2 | 2018 | 3.5 |
2 | 2019 | 3.5 |
2 | 2020 | 3.5 |
2 | 2021 | 3.5 |