Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7774693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:26:34+00:00 2026-06-01T17:26:34+00:00

I have a problem to order a query as a tree as this WITH

  • 0

I have a problem to order a query as a tree as this

WITH UtHierarchy
AS (
    SELECT etabid
        ,ut
        ,utlib
        ,parenteut
        ,0 AS LEVEL
        ,ut AS root
    FROM RUT
    WHERE etabid = 1
        AND parenteut IS NULL

    UNION ALL

    SELECT RUT.etabid
        ,RUT.ut
        ,RUT.utlib
        ,RUT.parenteut
        ,LEVEL + 1 AS LEVEL
        ,RUT.parenteut AS root
    FROM RUT
    INNER JOIN UtHierarchy uh ON uh.ut = rut.parenteut
    WHERE RUT.ETABID = 1
    )
SELECT *
FROM UtHierarchy
ORDER BY root

I need to have the following tree :

UT Root
UT Root
-- UT level 1
UT Root
-- UT level 1
-- -- UT level 2
UT Root

This is working as intended for level 0 or 1, but for higher level it s broken. I try to select in root column the ‘level 0’ parent to order by root and ut, but after some time on this problem, I can’t 🙁

How to resolve this ?

Thanks for your help.

EDIT : Thank you for editing with sql colors 🙂
I’ve seen the solution for the topmost level but user has deleted his post.

WITH UtHierarchy 
AS (
  SELECT etabid
  ,ut
  ,utlib
  ,parenteut,
  0 as profondeur,
  ut as root
  FROM RUT 
  where etabid = 278
  and parenteut is null
  UNION  ALL
  SELECT RUT.etabid
  , RUT.ut
  , RUT.utlib
  , RUT.parenteut
  , profondeur + 1 as profondeur
  , root as root 
  FROM RUT 
  inner join UtHierarchy uh on uh.ut = rut.parenteut
  where RUT.ETABID = 278
)
select ut, parenteut, profondeur, root 
from UtHierarchy
order by root

but it’s not working too

Here an example with true data

ut  parenteutlevel  root
10  1   1   1
11  1   1   1
12  1   1   1
13  1   1   1
14  1   1   1
130 13  2   1
131 13  2   1
132 13  2   1
133 13  2   1
134 13  2   1
135 13  2   1
136 13  2   1
120 12  2   1
121 12  2   1
122 12  2   1
110 11  2   1
111 11  2   1
112 11  2   1
113 11  2   1
114 11  2   1
115 11  2   1
116 11  2   1
101 10  2   1
102 10  2   1
103 10  2   1
104 10  2   1
105 10  2   1
106 10  2   1
107 10  2   1
1       0   1

As you can see it is not the good structure.
I need a tree as this :

ut  parenteutlevel  root
1       0   1
10  1   1   1
101 10  2   1
102 10  2   1
103 10  2   1
104 10  2   1
105 10  2   1
106 10  2   1
107 10  2   1
11  1   1   1
110 11  2   1
111 11  2   1
112 11  2   1
113 11  2   1
114 11  2   1
115 11  2   1
116 11  2   1
12  1   1   1
120 12  2   1
121 12  2   1
122 12  2   1
13  1   1   1
130 13  2   1
131 13  2   1
132 13  2   1
133 13  2   1
134 13  2   1
135 13  2   1
136 13  2   1
14  1   1   1    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:26:35+00:00Added an answer on June 1, 2026 at 5:26 pm

    ince the Recursive call is correct, your problem lies in the sorting of the result

    ORDER BY root

    You can try to create a sort path to help getting them in the right order:

    WITH UtHierarchy
    AS (
        SELECT etabid
            ,ut
            ,utlib
            ,parenteut
            ,0 AS LEVEL
            ,ut AS root
            ,RIGHT('000000' + CAST(ut AS varchar(MAX)), 6) AS sort
        FROM RUT
        WHERE etabid = 1
            AND parenteut IS NULL
    
        UNION ALL
    
        SELECT RUT.etabid
            ,RUT.ut
            ,RUT.utlib
            ,RUT.parenteut
            ,LEVEL + 1 AS LEVEL
            ,RUT.parenteut AS root
            ,uh.sort+'/'+RIGHT('000000' + CAST(RUT.ut AS varchar(20)), 6) AS sort
        FROM RUT
        INNER JOIN UtHierarchy uh ON uh.ut = rut.parenteut
        WHERE RUT.ETABID = 1
        )
    SELECT *
    FROM UtHierarchy
    ORDER BY sort
    

    Edit:

    CASE => CAST (Spelling mistake)

    Edit 2 (Adding a working example from your test data):

    Here you have a copy and paste test code. Works fine for me:

    SELECT 10 AS ut, 1 AS parenteut
    INTO #RUT
    UNION ALL SELECT 11, 1
    UNION ALL SELECT 12, 1
    UNION ALL SELECT 13, 1
    UNION ALL SELECT 14, 1
    UNION ALL SELECT 130, 13
    UNION ALL SELECT 131, 13
    UNION ALL SELECT 132, 13
    UNION ALL SELECT 133, 13
    UNION ALL SELECT 134, 13
    UNION ALL SELECT 135, 13
    UNION ALL SELECT 136, 13
    UNION ALL SELECT 120, 12
    UNION ALL SELECT 121, 12
    UNION ALL SELECT 122, 12
    UNION ALL SELECT 110, 11
    UNION ALL SELECT 111, 11
    UNION ALL SELECT 112, 11
    UNION ALL SELECT 113, 11
    UNION ALL SELECT 114, 11
    UNION ALL SELECT 115, 11
    UNION ALL SELECT 116, 11
    UNION ALL SELECT 101, 10
    UNION ALL SELECT 102, 10
    UNION ALL SELECT 103, 10
    UNION ALL SELECT 104, 10
    UNION ALL SELECT 105, 10
    UNION ALL SELECT 106, 10
    UNION ALL SELECT 107, 10
    UNION ALL SELECT 1, 0;
    
    WITH UtHierarchy
    AS (
        SELECT
            ut
            ,parenteut
            ,0 AS LEVEL
            ,ut AS root
            ,RIGHT('000000' + CAST(ut AS varchar(MAX)), 6) AS sort
        FROM #RUT
        WHERE
            parenteut = 0
    
        UNION ALL
    
        SELECT 
            RUT.ut
            ,RUT.parenteut
            ,LEVEL + 1 AS LEVEL
            ,RUT.parenteut AS root
        ,uh.sort+'/'+RIGHT('000000' + CAST(RUT.ut AS varchar(20)), 6) AS sort
        FROM #RUT AS RUT
        INNER JOIN UtHierarchy uh ON uh.ut = rut.parenteut    
        )
    SELECT *
    FROM UtHierarchy
    ORDER BY sort
    
    DROP TABLE #RUT;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with this SQL query: (SELECT tb1.id,tb1.bdate,tb1.jumpCard,tb1.publicImage,tb1.lastlogin FROM users AS tb1, online
I have a problem in my select statement in my query. This sql query
I have problem with optimize this query: SET @SEARCH = dokumentalne; SELECT SQL_NO_CACHE `AA`.`version`
I have an MySQL query like this: SELECT MAX(messages.ID) as maxID, messages.from, messages.read, users.userName
I have an index problem with my order by statement. I have this query
i have a problem to understand what happens in this query: $db->select(select r.id, m.mailing_id,
I have some problem to order an array by a field of this, here
I have problem with my MySQL query: include '../inc/mysql_config.php'; $sql=INSERT INTO ordrar (id, order,
I have a problem with query in C#. I have this part of code
I have a problem with sql query in php: select user, name, outlet, switch,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.