Given that I have this resultset structure (superfluous fields have been stripped)
Id | ParentId | Name | Depth
----------------------------
is it possible to have the records returned in tree order i.e. Parent
then Children
, if a Child
is a Parent
, then their Children
, if not then Sibling
, etc? For example,
Id | ParentId | Name | Depth
----------------------------
1 NULL Major 1
2 1 Minor 2
3 1 Minor 2
4 3 Build 3
5 3 Build 3
6 1 Minor 2
/* etc, etc */
The only way that I can think of doing this would be to follow this article -
Improve hierarchy performance using nested sets
and include [LeftExtent]
and [RightExtent]
fields against each record. Now the SQL in the article works fine when Ids
are unique, but in this particular tree structure, a record with the same Id
can appear in different places within the tree (the ParentId
field is different, obviously). I think the problem is in this SQL from the article -
INSERT INTO @tmpStack
(
EmployeeID,
LeftExtent
)
SELECT TOP 1 EmployeeID, @counter
FROM Employee
WHERE ISNULL(ParentID, 0) = ISNULL(@parentid,0)
/* If the Id has already been added then record is not given [LeftExtent] or [RightExtent] values. */
AND EmployeeID NOT IN (SELECT EmployeeID FROM @tmpStack)
How can this be altered to allow records with duplicate Ids
to be given [LeftExtent] and [RightExtent] values, or I am completely missing an easier way to return the resultset in the order that I require?