I have the following two tables in my database. One holds transaction data and the other holds development periods.
Year, DevelopmentPeriod, Amount 2018, 9, 13200 2018, 10, 13200 2018, 11, 17625 2018, 12, 17625 2018, 13, 13200.0165 2018, 14, 13200.0165 2018, 15, 13200.0165 UnderwritingYear, DevelopmentPeriod 2018, 1 2018, 2 2018, 3 2018, 4 2018, 5 2018, 6 2018, 7 2018, 8 2018, 9 2018, 10 2018, 11 2018, 12 2018, 13 2018, 14 2018, 15 2018, 16 2018, 17 2018, 18 2018, 19 2018, 20 2018, 21 2018, 22 2018, 23
How can I extrapolate the data into the future development periods like below?
Year, DevelopmentPeriod, Amount 2018, 16, 13200.0165 2018, 17, 13200.0165 ... 2018 22, 13200.0165 2018 23, 13200.0165
Advertisement
Answer
One method uses cross apply:
select t2.*, t1.amount
from table2 t2 cross apply
(select top (1) t1.*
from table1 t1
where t1.year = t2.UnderwritingYear and
t1.DevelopmentPeriod <= t2.DevelopmentPeriod
order by t1.DevelopmentPeriod desc
) t1;