Build varchar
sort key recursively and sort descending with a trick so that shorter values are considered greater.
WITH sorted AS
(
SELECT
right(cast (1000000 + (select coalesce(max(t2.id),t.id) from tree_table t2 where t2.parent_id = t.id )as varchar(max)),6) AS [sort_key],
t.id,
t.parent_id,
t.name
FROM
tree_table t
),
rcte AS (
SELECT *
FROM sorted t
WHERE
t.parent_id = 0
UNION ALL
SELECT
r.sort_key + t.sort_key,
t.id,
t.parent_id,
t.name
FROM
sorted t
JOIN
rcte r ON r.id = t.parent_id
)
SELECT *
FROM rcte
ORDER BY
stuff(replicate(cast ('9' as varchar(max)), (select max(len(r2.sort_key)) from rcte r2)),1,len(sort_key), sort_key)
DESC;
I assumed id <1000000 for simplicity, you can easily adjust it to any big value with bigint
arithmetic.
Output :
sort_key id parent_id name
000019 16 0 4Title
000019000020 17 16 ㄴRE 4Title 1-1
000019000020000020 20 17 ㄴRE 4Title 1-1-1
000019000019 19 16 ㄴRE 4Title 1-2
000014 1 0 1Title
000014000015 6 1 ㄴRE 1Title 1-3
000014000015000018 15 6 ㄴRE 3Title 1-3-1
000014000015000018000018 18 15 ㄴRE 3Title 1-3-1-1
000014000014 14 1 ㄴRE 1Title 1-4
000014000012 4 1 ㄴRE 1Title 1-1
000014000012000012 12 4 ㄴRE 1Title 1-1-3
000014000012000011 11 4 ㄴRE 1Title 1-1-2
000014000012000010 10 4 ㄴRE 1Title 1-1-1
000014000005 5 1 ㄴRE 1Title 1-2
000013 3 0 3Title
000013000013 13 3 ㄴRE 3Title 1-1
000009 2 0 2Title
000009000009 9 2 ㄴRE 2Title 1-3
000009000008 8 2 ㄴRE 2Title 1-2
000009000007 7 2 ㄴRE 2Title 1-1
CLICK HERE to find out more related problems solutions.