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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:23:18+00:00 2026-06-02T15:23:18+00:00

i am using CTE to manage my hierarchy data. Below is my select query

  • 0

i am using CTE to manage my hierarchy data. Below is my select query

 WITH ctLevel
                AS
                (
                    SELECT
                        C_TASK_ID                                                               AS Child
                        ,P_Task_ID                                                              AS Parent
                        ,common_task.   TASK_SEQ                                                AS taskSeq
                        ,1                                                                      AS [Level]
                        ,CAST( TASK_SEQ AS VARCHAR(MAX))                                        AS [taskOrder]
                        ,CAST (Replicate(TASK_SEQ,  1)+' '+ TASK_NAME  AS VARCHAR(MAX))  AS [Task_Name]
                        ,common_task.TASK_POSITION                                              AS [TASK_POSITION]
                    FROM   
                        [COMMON.TASK_REL] as common_task_rel, 
                        [COMMON.TASK] as common_task
                    WHERE  
                        common_task_rel.C_TASK_ID = common_task.TASK_ID
                        and     common_task.[TASK_TYPE] = 'F' AND common_task.[MODULE_CODE] ='PRODE' AND common_task.[STATUS] <> 'D'
                        and     common_task_rel.P_TASK_ID is null

                    UNION ALL

                    SELECT 
                        C_TASK_ID                                                                   AS Child
                        ,P_Task_ID                                                                  AS Parent
                        ,common_task.   TASK_SEQ                                                    AS taskSeq
                        ,[Level] + 1                                                                AS [Level]
                        ,[taskOrder] + '.' + CAST(TASK_SEQ AS VARCHAR(MAX))                         AS [taskOrder]
                          ,CAST (Replicate('&nbsp;&nbsp;&nbsp;', [Level] + 1) + taskOrder + '.' + CAST(TASK_SEQ AS VARCHAR(MAX))+' ' +common_task.TASK_NAME AS VARCHAR(MAX))  AS [Task_Name]
                        ,common_task.TASK_POSITION                                              AS [TASK_POSITION]
                    FROM   
                        [COMMON.TASK_REL]as common_task_rel
                    INNER JOIN 
                        ctLevel
                    ON 
                        ( P_Task_ID = Child ) ,
                        [COMMON.TASK] as common_task

                    WHERE  
                            common_task_rel.C_TASK_ID = common_task.TASK_ID
                    and     common_task.[TASK_TYPE] = 'F' AND common_task.[MODULE_CODE] = 'PRODE' AND common_task.[STATUS] <> 'D'
                    )

                   SELECT [Parent] ,[Child],taskSeq, Level,[taskOrder],Task_Name,[TASK_POSITION]
                    FROM   ctLevel  
                    order by [taskOrder],[Level],[TASK_POSITION]

How my result look like:

enter image description here

You may c the example.The
10
10.1
10.1.1
10.1.1.1
suppose will be at the bottom of the 9

Is it possible to sort those record like below

1
2
2.1
2.2
2.4
2.5
2.6
2.7
3
3.1
3.2
4
4.1
4.2
5
5.1
6
6.1
6.2
6.3
6.4
6.5
7
8
9
9.1
9.1
9.1.1
9.1.1.1
9.1.1.2
9.1.1.3
9.1.1.4

10
10.1
10.1.1
10.1.1.1

Thanks in advance.

  • 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-02T15:23:20+00:00Added an answer on June 2, 2026 at 3:23 pm

    Format the numbers like 001, for example:

    right('000' + convert(varchar(max), [Level] + 1),3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MS SQL 2005 and I have created a CTE query to return
In the code below I am using a recursive CTE(Common Table Expression) in SQL
Using PHP, I can convert MySQL data or static table data to csv, Excel,
I am planning on using a recursive table in my data model so that
Consider the following requirement for building a forum App Parent Post - Child Post1
I'm trying to run a query from ADO.NET using SQL Server 2008R2. I'm using
Is there any performance gain using a CTE over a derived table?
I have a view that returns 2 ints from a table using a CTE.
I need to keep a table in sync with a recursive CTE query that
Say that have the following CTE that returns the level of some tree data

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.