Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling the sibling order under recursive CTE?

I have a CTE query which looks for main leafs and sub leafs. but I'm having trouble controling the leaf selection order between 2 siblings :

Each row in the table is declared as :

(childID INT ,parentID INT ,NAME NVARCHAR(30),location int)

Where location is a priority to sort IFF they are siblings.

And so I have this tree structure : those pairs has a location priority :

enter image description here

For example :

`a` ( location=1) should be before `f` (location=2)
`b` ( location=1) should be before `e` (location=2)
`d` ( location=1) should be **before** `c` (location=2)

The problem is that it seems that I must first order by childID in order to see the right structure ( sibling unsorted).

But - what how does my order by should look like so I will be able to see the right structure (&& sibling sorted) ?

(in my example : d should come before c)

Here is the working query which yields all the tree leafs ( unsorted siblings)

p.s. childID is not indicating anything about the sorting. it's just a placeholder. as I said , the location between 2 brothers is by the location column.( here , childId is sorted because thats the order of which i inserted the rows...

like image 768
Royi Namir Avatar asked Jan 12 '23 17:01

Royi Namir


1 Answers

You can calculate path of the tree node in your CTE and use it for sorting

;WITH CTE AS(
   SELECT childID, parentID, 0 AS depth, NAME , location,
         cast(location as varbinary(max)) path
   FROM   @myTable
   WHERE   childID = parentID 
    UNION ALL
    SELECT  TBL.childID, TBL.parentID,
           CTE.depth + 1 , TBL.name ,TBL.location,
           cte.path + cast(TBL.location as binary(4))
    FROM   @myTable AS TBL
            INNER JOIN CTE  ON  TBL.parentID = CTE.childID
    WHERE   TBL.childID<>TBL.parentID
)
SELECT depth, childID, parentID, location, REPLICATE('----', depth) + name
FROM CTE
ORDER BY path
like image 134
i-one Avatar answered Jan 21 '23 20:01

i-one