I have a table which implements a tree using hierarchyid column
Sample data:
People \
Girls \1\
Zoey \1\1\
Kate \1\2\
Monica \1\3\
Boys \2\
Mark \2\1\
David \2\2\
This is the order using hierarchyid column as sort column
I would like to sort data using hierarchyid but also using name so it would look like this:
People \
Boys \2\
David \2\2\
Mark \2\1\
Girls \1\
Kate \1\2\
Monica \1\3\
Zoey \1\1\
Is there a simple solution to do this?
Can it be done with a single SQL
query?
Rewrite your query as a recursive CTE
:
DECLARE @table TABLE (id INT NOT NULL PRIMARY KEY, name NVARCHAR(4000) NOT NULL, path HIERARCHYID)
INSERT
INTO @table
VALUES
(1, 'People', '/'),
(2, 'Girls', '/1/'),
(3, 'Boys', '/2/'),
(4, 'Zoey', '/1/1/'),
(5, 'Kate', '/1/2/'),
(6, 'Monica', '/1/3/'),
(7, 'Mark', '/2/1/'),
(8, 'David', '/2/2/')
;WITH q AS
(
SELECT *, HIERARCHYID::Parse('/') AS newpath
FROM @table
WHERE path = HIERARCHYID::GetRoot()
UNION ALL
SELECT t.*, HIERARCHYID::Parse(q.newpath.ToString() + CAST(ROW_NUMBER() OVER (ORDER BY t.name) AS NVARCHAR(MAX)) + '/')
FROM q
JOIN @table t
ON t.path.IsDescendantOf(q.path) = 1
AND t.path.GetLevel() = q.path.GetLevel() + 1
)
SELECT *
FROM q
ORDER BY
newpath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With