Parent sql child tree with sort order

Assuming this table

ParentId    ChildId    SortOrder
---------------------------------
0           1          0
1           2          1
2           3          1
2           4          2
2           6          3
1           7          2
1           9          3
9           10         1
9           12         2

how can I write a simple CTE (or another type of request) that returns the parent / child relationship at any level, but preserves the sort order for all children below the parent.

in principle, this would be a simple tree view with sub-items ordered in a specific sort order.

sql server 2008 is db

update: there may be infinite parent / child levels - I tried some examples from other questions / answers of the stack, but none of them included sorting order at the level of children.

An example result should be:

parent (sort 0)
    child (sort 1)
    child (sort 2)
    child (sort 3)
        child-child (sort 1)
    child (sort 4)
        child-child (sort 1)
        child-child (sort 2)

and etc.

I hope this makes sense - maybe I'm looking at it wrong, and is it better to build this kind of result in the middle of the level?

,

+1
2

CTE . 2 ( , )

declare @relations table(ParentID int, ChildID int, SortOrder int, treeID int);

insert into @relations values
(0,1,0,0), (1,2,1,0), (2,3,2,0), (2,4,1,0), (2,6,3,0), (1,7,2,0), (1,9,3,0), (9,10,1,0), (9,12,2,0) --tree 0
, (0,1,0,1), (1,2,1,1), (2,3,2,1), (2,4,1,1), (2,6,3,1), (1,7,2,1), (1,9,3,1), (9,10,1,1), (9,12,2,1) --tree 1

; with cte(ParentId,ChildId,SortOrder,depth,agg,treeID) as (
    select null,ParentId,SortOrder,0
    , right('0000000'+CAST(treeID as varchar(max)),7)
        +right('0000000'+CAST(SortOrder as varchar(max)),7)
    , treeID
    from @relations where ParentId=0
    union all
    select cte.ChildId,r.ChildId,r.SortOrder,cte.depth+1
    , cte.agg
        +right('0000000'+CAST(r.treeID as varchar(max)),7)
        +right('0000000'+CAST(r.SortOrder as varchar(max)),7)
        +right('0000000'+CAST(r.ChildId as varchar(max)),7)
    , r.treeID
    from cte
    inner join @relations r on r.ParentID=cte.ChildId
    where cte.depth<32767
    and r.treeID=cte.treeID
)
select
tree=case depth when 1 then cast(ParentID as varchar(30))+' (sort '+cast(SortOrder as varchar(30))+')'
    else REPLICATE(CHAR(9),depth-1)
        + cast(ChildId as varchar(30))+' (sort '+cast(SortOrder as varchar(30))+')'
    end
from cte
where depth>0
order by agg
option (maxrecursion 32767);

:

tree
--------------------------------------------------
0 (sort 0)
    2 (sort 1)
        4 (sort 1)
        3 (sort 2)
        6 (sort 3)
    7 (sort 2)
    9 (sort 3)
        10 (sort 1)
        12 (sort 2)
0 (sort 0)
    2 (sort 1)
        4 (sort 1)
        3 (sort 2)
        6 (sort 3)
    7 (sort 2)
    9 (sort 3)
        10 (sort 1)
        12 (sort 2)
+7

, .

0

All Articles