with cte as
(
select age
,max(period - age) over () + age as period
,max(yr - age) over() + age as yr
from #yourtable
)
select age
,case when period >0 then period end as period
,yr
from cte
age
period
yr
0
2000
1
2001
2
2002
3
2003
4
2004
5
2005
6
2006
7
2007
8
2008
9
2009
10
2010
11
2011
12
2012
13
2013
14
2014
15
2015
16
2016
17
2017
18
2018
19
2019
20
1
2020
21
2
2021
22
3
2022
23
4
2023
24
5
2024
25
6
2025
26
7
2026
27
8
2027
28
9
2028
29
10
2029
30
11
2030
…
Warning: Null value is eliminated by an aggregate or other SET operation.
hidden batch(es)
; WITH PeriodInc AS (SELECT TOP 1 Period - Age AS PeriodInc FROM #yourtable WHERE Period IS NOT NULL),
YrInc AS (SELECT TOP 1 Yr - Age AS YrInc FROM #yourtable WHERE Yr IS NOT NULL)
SELECT Age,
CASE WHEN (Age + PeriodInc) >= 0 THEN (Age + PeriodInc) ELSE NULL END AS Period,
Age + YrInc AS Yr
FROM #yourtable
CROSS JOIN PeriodInc
CROSS JOIN YrInc