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

  • SEARCH
  • Home
  • 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 273287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:26:05+00:00 2026-05-12T00:26:05+00:00

Given that I have this resultset structure (superfluous fields have been stripped) Id |

  • 0

Given that I have this resultset structure (superfluous fields have been stripped)

Id | ParentId | Name | Depth
----------------------------

is it possible to have the records returned in tree order i.e. Parent then Children, if a Child is a Parent, then their Children, if not then Sibling, etc? For example,

Id | ParentId | Name | Depth
----------------------------
1    NULL       Major    1
2    1          Minor    2
3    1          Minor    2
4    3          Build    3
5    3          Build    3
6    1          Minor    2

/* etc, etc */

The only way that I can think of doing this would be to follow this article –

Improve hierarchy performance using nested sets

and include [LeftExtent] and [RightExtent] fields against each record. Now the SQL in the article works fine when Ids are unique, but in this particular tree structure, a record with the same Id can appear in different places within the tree (the ParentId field is different, obviously). I think the problem is in this SQL from the article –

  INSERT INTO @tmpStack
    (
      EmployeeID, 
      LeftExtent
    )
  SELECT TOP 1 EmployeeID, @counter 
  FROM Employee 
  WHERE ISNULL(ParentID, 0) = ISNULL(@parentid,0) 
  /* If the Id has already been added then record is not given [LeftExtent] or [RightExtent] values. */
  AND EmployeeID NOT IN (SELECT EmployeeID FROM @tmpStack) 

How can this be altered to allow records with duplicate Ids to be given [LeftExtent] and [RightExtent] values, or I am completely missing an easier way to return the resultset in the order that I require?

  • 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-05-12T00:26:06+00:00Added an answer on May 12, 2026 at 12:26 am

    Here’s one that does the trick for me:

    @ParentID is just a starting point in the hierarchy, but you can pass in 0 (but I think you’re using null as the base ID, so you’ll get the idea)

    The key to ordered sorting is with the sort key that’s built up.

    WITH RoleHierarchy (RoleID, [Role], [Description], ParentID, Editable, HierarchyLevel, SortKey) AS
    (
       -- Base
       SELECT
            RoleID,
            [Role],
            [Description],
            ParentID,
            Editable,
            0 as HierarchyLevel,
            CAST(RoleID AS VARBINARY(300))
       FROM
            dbo.Roles       
       WHERE
            RoleID = @ParentID
    
       UNION ALL
    
       -- Recursive
       SELECT
            e.RoleID,
            e.[Role],
            e.[Description],
            e.ParentID,
            e.Editable,
            th.HierarchyLevel + 1 AS HierarchyLevel,
            CAST (th.SortKey + CAST (e.[Role] AS VARBINARY(100)) + CAST (e.[RoleID] AS VARBINARY(100)) AS VARBINARY(300))
       FROM
            Roles e
            INNER JOIN RoleHierarchy th ON e.ParentID = th.RoleID
        WHERE
            e.RoleID != 0
    )
    
    SELECT
        RoleID,
        ParentID,
        [Role],
        [Description],
        Editable,
        HierarchyLevel
    FROM
        RoleHierarchy
    WHERE
        RoleID != @ParentID
    ORDER BY
        SortKey
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer f ∈ O(g) says, essentially For at least one choice… May 12, 2026 at 10:09 am
  • Editorial Team
    Editorial Team added an answer The one to use depends on how you created the… May 12, 2026 at 10:09 am
  • Editorial Team
    Editorial Team added an answer I try to attempt to explain normalization in layman terms… May 12, 2026 at 10:09 am

Related Questions

I'm not quite sure if this is possible, or falls into the category of
I've got a very large xml data set that is structured like the following:
This is for MySQL and PHP I have a table that contains the following
Hopefully someone can shed a little light on an issue that I'm currently having

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.