clear markdown feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3364508 fiddles created (36312 in the last week).

CREATE TABLE #yourtable (Age int, Period int, Yr int) INSERT INTO #yourtable (Age, Period, Yr) VALUES (0 , NULL, NULL), (1 , NULL, NULL), (2 , NULL, NULL), (3 , NULL, NULL), (4 , NULL, NULL), (5 , NULL, NULL), (6 , NULL, NULL), (7 , NULL, NULL), (8 , NULL, NULL), (9 , NULL, NULL), (10, NULL, NULL), (11, NULL, NULL), (12, NULL, NULL), (13, NULL, NULL), (14, NULL, NULL), (15, NULL, NULL), (16, NULL, NULL), (17, NULL, NULL), (18, NULL, NULL), (19, NULL, NULL), (20, NULL, NULL), (21, 46 , 2065), (22, NULL, NULL), (23, NULL, NULL), (24, NULL, NULL), (25, NULL, NULL), (26, 51 , 2070), (27, NULL, NULL), (28, NULL, NULL), (29, NULL, NULL), (30, NULL, NULL);
31 rows affected
 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
Age Period Yr
0 25 2044
1 26 2045
2 27 2046
3 28 2047
4 29 2048
5 30 2049
6 31 2050
7 32 2051
8 33 2052
9 34 2053
10 35 2054
11 36 2055
12 37 2056
13 38 2057
14 39 2058
15 40 2059
16 41 2060
17 42 2061
18 43 2062
19 44 2063
20 45 2064
21 46 2065
22 47 2066
23 48 2067
24 49 2068
25 50 2069
26 51 2070
27 52 2071
28 53 2072
29 54 2073
30 55 2074
 hidden batch(es)