Needing to convert a flat structure to a parent child table. Have large sets of data with a sequential id and a level column. The top level contains multiple second levels. The last level is not set. The data looks like the following:
id group level name
1 G1 1 A1
2 G1 2 A2
3 G1 2 A3
4 G1 3 A4
5 G1 4 A5
6 G1 2 A2
7 G1 3 A3
8 G1 3 A5
9 G1 2 A6
So 1 will be the parent of all the twos under it and a new section of ones can exist in G1. Same at every level. So I want to fill in the parent id for each level. So all the twos would be id 1. The id 4 ouls have 3 as it parent. Id 7 would have 6 as its parent. So need complete details on a recursive way to populate the parent or what is the fastest was with out going through line by line..