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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:04:44+00:00 2026-06-12T03:04:44+00:00

I have a CTE Recursive query which returns the output as expected, however It

  • 0

I have a CTE Recursive query which returns the output as expected, however It would be nice if I can control the sort. For example I would like to sort PARENT nodes by using a custom SORT column in table…and IF POSSIBLE sort the nodes under Parent Categories as well by using a SORT column.

Hete is SQL Fiddle

Please look at sort Column, With Sort output should be something like this.

 Appliances < -- Parent
   Dryers
   Washers
 Toys < -- Parent
 Furniture < -- Parent

if sorting on child nodes is NOT possible then its fine, but it would be nice to atleast control the sort for PARENT nodes.

  • 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-12T03:04:45+00:00Added an answer on June 12, 2026 at 3:04 am

    EDIT

    Try this for the latest question/fiddle edit. Same idea, but one more ISNULL, as I forgot to handle NULL sorts in the recursive part.

    ;WITH cte AS (
        SELECT 0 AS lvl, catcode, catName, parentID,
            CAST(catCode AS VARCHAR(max)) AS Path,
            CAST(isnull(sort,9999) AS VARCHAR(max)) AS SortPath
        FROM categories WHERE parentID =0
        UNION ALL
        SELECT p.lvl + 1, c.catCode, c.catName, c.parentID,
            p.Path + '_' + CAST(c.catCode AS VARCHAR(max)),
            p.SortPath + '_' + CAST(isnull(c.sort,9999) AS VARCHAR(max))
        FROM categories c
        INNER JOIN cte p ON p.catCode = c.parentID
    )
    SELECT 
        catCode, 
        catName AS catName, 
        lvl,
        Path,
        parentID
    FROM cte 
    ORDER BY SortPath
    

    Note: A single varchar(max) within a series of string concatenations makes the end result a varchar(max). A single cast will do.

    FYI – for future SQL questions, it would be really great if you could prepare a SQLFiddle schema, and what query you have thus far. This allows others to spend minimal time on setup. It also helps the discussion if you had to expand on the sample or provide what if query variants.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query with a CTE that returns multiple rows, I want to
I have a problem with recursive CTE query Let's say that I have that
I have a CTE-based query in which I retrieve hourly intervals between two given
I've cretaed a TVF which returns a table with parent-records from a recursive CTE
I'm able to generate a hierarchy with a recursive CTE but I have always
I'm using MS SQL 2005 and I have created a CTE query to return
Have deployed numerous report parts which reference the same view however one of them
Say that have the following CTE that returns the level of some tree data
I have the following recursive function: ALTER FUNCTION [dbo].[ListAncestors] ( @Id int ) RETURNS
I have the following query: WITH cte AS ( SELECT windowId, frameIndx, elemIndx, comment,

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.