Hello,
I have a SQL Server table uses the "nested sets" approach (ala Joe Celko) to storing data as a hierarchy in a static table. This has worked wonderfully for us for two years, but now I am faced with the challenge of an element having multiple parents... and I could use some advice.
From what I have read, most hierarchical schemes assume that a child resides under one tree. In the Nested Sets, a node cannot span multiple trees due to the sequential nature of the "lft and rgt" columns. A very simple example:
PersonID, NAME, lft, rgt, Parent (un-needed, but a handy visual aid)
1, Roger, 1, 6,
2, Dolly, 2, 3, 1
3, Marie, 4, 5, 1
4, Paul, 7, 10,
5, Ed, 8, 9, 4
But what if Marie reports to both Roger AND Paul (or even more people)? Her "lfg and rgt" colums cannot represent duplicate membership. The idea I have is to remove the hierarchy colums (lft, rgt, Parent) from the above table and move them to another table which and joins to the above table as a one-to-many relationship as follows:
TreeID, PersonID, lft, rgt, Parent
1, 1, 1, 6,
2, 2, 2, 3, 1
3, 3, 4, 5, 1
4, 4, 7, 12,
5, 5, 8, 9, 4
6, 3, 10, 11, 4 <-- Second entry for Marie (ID 3)
Marie still only has one "node", but it is located under two different trees. Does this sound like a valid solution? Am I missing something here that will give me stress further down the road?
Thank you
I have a SQL Server table uses the "nested sets" approach (ala Joe Celko) to storing data as a hierarchy in a static table. This has worked wonderfully for us for two years, but now I am faced with the challenge of an element having multiple parents... and I could use some advice.
From what I have read, most hierarchical schemes assume that a child resides under one tree. In the Nested Sets, a node cannot span multiple trees due to the sequential nature of the "lft and rgt" columns. A very simple example:
PersonID, NAME, lft, rgt, Parent (un-needed, but a handy visual aid)
1, Roger, 1, 6,
2, Dolly, 2, 3, 1
3, Marie, 4, 5, 1
4, Paul, 7, 10,
5, Ed, 8, 9, 4
But what if Marie reports to both Roger AND Paul (or even more people)? Her "lfg and rgt" colums cannot represent duplicate membership. The idea I have is to remove the hierarchy colums (lft, rgt, Parent) from the above table and move them to another table which and joins to the above table as a one-to-many relationship as follows:
TreeID, PersonID, lft, rgt, Parent
1, 1, 1, 6,
2, 2, 2, 3, 1
3, 3, 4, 5, 1
4, 4, 7, 12,
5, 5, 8, 9, 4
6, 3, 10, 11, 4 <-- Second entry for Marie (ID 3)
Marie still only has one "node", but it is located under two different trees. Does this sound like a valid solution? Am I missing something here that will give me stress further down the road?
Thank you