The Setup file below contains all the data that is needed to answer the question.
Sample file
End result
Problem: As you can see in the Sample File Screenshot I have inherited a database that stores the EffectiveDates for when an Entity began their association with a specific EntityLocation. In the example image Entity 10170's most recent association with an EntityLocation was 2011-01-01, before that it was 2006-01-01, and so on... (See Sample File Screenshot)
Question: Using a Recursive CTE on the sample data I would like the return result to look like the End Result Sample, how do i accomplish this using Recursive CTE and NOT Cursors. (See End Result Sample)
Setup File: sample xlsx which contains the full data.
This reaction is not about CTE, but about getting you to a solution for this particular problem. You do not need recursion here. Recursion is only neccessary when you have to iterate a link to itself multiple times. If you want to study CTE try to make e.g. an organogram.
Your solution is about linking the EffectiveDate as an Opendate to another EffectiveDate as a Closedate if there is one.
First you may want to create an Location index per entity using the ROW_NUMBER() function with partitioning. So my new table
location
will be defined asNow you can link the effectivedate for each location (equal entityId's) to a next effectivedate of the location (LocId+1) if there is one (LEFT JOIN).
Which results in:
as per your request.
It could be a suggestion not to replace the NULL value at all. In accordance to @Jack's reaction you then do not have to dream up a future date. The result simple tells you there is no closedate to this EntityLocation.
It's simple to select the actual locations by selecting the NULL value in this column. The result would be: