I have a table as follows:
ID IsCreated ParentID
101 0 NA
102 1 101
103 0 NA
104 1 102
105 1 104
I want to add a new col called 'OGParentID' which looks results as follows:
ID IsCreated ParentID OGParentID
101 0 NA 101
102 1 101 101
103 0 NA 103
104 1 102 101
105 1 104 101
The logic used here is as follows: For each row, check if IsCreated = 1, if yes, then lookup the ParentID in the ID columns to check if it's IsCreated is 0, if yes then set OGParentID to the looked up Parent ID else, check the looked up ParentID's Parent ID and continue the loop.