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 8571111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:42:41+00:00 2026-06-11T18:42:41+00:00

I have a Standard table, which sotres parent, child category relationship…like this. id, parent,

  • 0

I have a Standard table, which sotres parent, child category relationship…like this.

id, parent, catName, sort

And I use the following query to create a recursive tree

;WITH cte AS (
    SELECT 0 AS lvl, id, catName, parent,levels,sort,
        CAST(id AS VARCHAR(128)) AS path
    FROM CategoriesMap WHERE parent =0
    UNION ALL
    SELECT p.lvl + 1, c.id, c.catName, c.parent,c.levels,c.sort,
        CAST(p.path + '_' + CAST(c.id AS VARCHAR) AS VARCHAR(128))
    FROM CategoriesMap c
    INNER JOIN cte p ON p.id = c.parent
)
SELECT 
    id, 
    catName AS catName, 
    lvl,
    levels,
    path,
    parent,
    sort
FROM cte 
ORDER BY path

And the output is like this Image:

enter image description here

Look for the Row with value ASP.NET & CLASSIC ASP, these are the last leaf(children) for the technology > Software (parents), I want to sort the LAST CHILDREN of any given parent (last parent). I can have multiple parents for a given node (last child) & all I care about is sort the LAST Children (leaf) using the “Sort” column.

so basicly “Classic Asp” shoud be before “Asp.Net” (Last Column is SORT column in my image).

My query is fine, it returns the results as expected…only challenege is i want to SORT the last NODE using the SORT column in table, last node can have 3 or 4 children which I want to sort, all nodes above the last node are its parents (which are in correct order already).

I want output like this…. Internet > ISP’s > CableVision (1) :
Verizon (2) as you can see CableVision & Verizon have Sort Value of 1
& then 2, Now lets say we have Shopping > Coupons > Macys(0) : Sears
(2), same thing….I want Macys & Sears to be sorted…and its pretty
obvious their parents are Shopping > Coupons.

@Richard aka cyberkiwi, after applying your code, my sorting for Categories table is very random. output is below
enter image description here

  • 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-11T18:42:42+00:00Added an answer on June 11, 2026 at 6:42 pm

    Delaying the calculation of the path for one level, so the final result set has the parent path (ppath) available:

    ;WITH cte AS (
        SELECT 0 AS lvl, id, catName, parent,levels,sort,
            CAST('' AS VARCHAR(128)) AS ppath
        FROM CategoriesMap WHERE parent =0
        UNION ALL
        SELECT p.lvl + 1, c.id, c.catName, c.parent,c.levels,c.sort,
            CAST(p.ppath + '_' + CAST(p.id AS VARCHAR) AS VARCHAR(128))
        FROM CategoriesMap c
        INNER JOIN cte p ON p.id = c.parent
    )
    SELECT 
        id, 
        catName, 
        lvl,
        levels,
        CAST(ppath + '_' + CAST(id AS VARCHAR) AS VARCHAR(128)) AS path,
        parent,
        sort
    FROM cte
    ORDER BY 
        CASE WHEN sort IS NULL
               THEN path
               ELSE ppath
        END
      , sort ;
    

    Not really sure why the above gives error. This will not:

    ORDER BY 
        CASE WHEN sort IS NULL
          THEN CAST(ppath + '_' + CAST(id AS VARCHAR) AS VARCHAR(128))
          ELSE ppath
        END
      , sort ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an html table that looks like this in a standard HTML 4.01
I have a standard table: I want to use jQuery to reformat the table,
Using Postgres v. 8.4.2. I have a standard three-table setup for a many-to-many relationship.
I have a table of items which is retrieved from the internet. This table
Suppose you have a standard org hierarchy table in Oracle. For simplicity sake, assume
I have a standard store schema with an InvoiceLine table that contains Products. I'd
I have an HTML table with collapsed and adjacent borders and a standard border
I have standard source code package under Linux which requires to run ./configure make
Is it possible to have standard war deployment, which can be deployed on tomcat
HI I have a table which holds files and their types such as CREATE

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.