How can I write a CTE in SQL Server to get the date from the preceding row for the person that has Type = NA.
If there are multiple preceding rows with NA then take the first one when sorted by dt_eff asc.
If there are any other types between start of record and type = NA then these records should be ignored. Only consider other Types for the same person when previous type is ended (Person 124 Scenario).
Source Data
Person Type dt_eff
123 A 2018-10-23 <Start of record >
123 NA 2018-11-19 <Should be the end date for above and don't select this in output>
123 NA 2018-12-25 <don't select this in output>
124 A 2020-01-01 <Start of record >
124 B 2020-02-15 <Ignore and don't select in output>
124 NA 2020-05-14 <Should be the end date for start of record and don't select in op>
124 C 2020-10-13 <As the above start record has ended this should be new start>
124 NA 2021-01-15 <should be the end date for second start record>
124 A 2021-05-22 <As the above start record has ended this should be new start>
124 T 2021-08-22 <Ignored and don't select in output>
456 NA 2022-04-19 <Ignore as there is no leg record with valid type>
456 A 2022-05-01 <Start of record and null as end date as there is no type = NA>
456 B 2022-07-15 <Ignore>
Expected Output
Person Type dt_start dt_end
123 A 2018-10-23 2018-11-19
124 A 2020-01-01 2020-05-14
124 C 2020-10-13 2021-01-15
124 A 2021-05-22 NULL
456 A 2022-05-01 NULL