I have a table with the columns Age
, Period
and Year
. The column Age
always starts with 0 and doesn't have a fixed maximum value (I used 'Age' 0 to 30 in this example but the range could also be 0 to 100 etc.), the values Period
and Year
only appear in certain rows at certain ages.
However at what Age
the values for Period
and Year
appear, changes and the solution should therefore be dynamic. What is the best way to fill in the NULL
values with correct Period
and Year
?
I am using SQL Server.
Age Period Year
-----------------
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
The result should look like this, the numbers for Period
and Year
should be increased and/or decrease from the last known values for Period
and Year
.
Age Period Year
-----------------
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
Here is an UPDATE to my question as I didn't specify my requirement detailed enough:
The solution should be able to handle different combinations of Age
, Period
and Year
. My start point will always be a known Age
, Period
and Year
combination. However, the combination Age
= 21, Period
= 46 and Year
= 2065 (or 26|51|2070 as the second combination) in my example is not static. The value at Age
= 21 could be anything e.g. Period
= 2 and Year
= 2021. Whatever the combination (Age
, Period
, Year
) is, the solution should fill in the gaps and finish the sequence counting up and down from the known values for Period
and Year
. If a Period
value sequence becomes negative the solutions should return NULL
values, if possible.