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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:22:27+00:00 2026-05-28T16:22:27+00:00

I am using T/SQL in Microsoft SQL Server 2008 I have a table CREATE

  • 0

I am using T/SQL in Microsoft SQL Server 2008

I have a table

CREATE TABLE [TestTable](
[CHILD] [int] NOT NULL,
[PARENT] [int] NOT NULL
) ON [PRIMARY]

GO

These are some values which define a parent child hierarchial relationship

CHILD PARENT
1       2
2       0
3       1
4       2
5       0

Visually, this table looks like this

0
   2
      1
         3
      4
   5

I would ideally like the values to be shown as follows (where the right hand column indicates the generation)

CHILD    GENERATION
 0          0
 2          1
 1          2
 3          3
 4          2
 5          1

My T/SQL code looks like this

with n(CHILD, PARENT, GENERATION) as (
select CHILD, PARENT,1 as GENERATION from TestTable
where PARENT=0 
union all
select nplus1.CHILD, nplus1.PARENT, GENERATION+1 from TestTable as nplus1, n
where nplus1.PARENT=n.CHILD 
)
select CHILD,GENERATION from n

However it doesn’t work!

It returns

CHILD   GENERATION
2        1
5        1
1        2
4        2
3        3

It has the right generation, but the wrong sort order!
Does anyone have any ideas how to solve this?

Thank you!

  • 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-28T16:22:28+00:00Added an answer on May 28, 2026 at 4:22 pm

    You’ll need your recursion to also build something that can be sorted by at the end:

    declare @t TABLE (
    [CHILD] [int] NOT NULL,
    [PARENT] [int] NOT NULL
    ) 
    
    insert @t values
    ( 0, -1),   -- I added this
    ( 1, 2 ),
    ( 2, 0 ),
    ( 3, 1 ),
    ( 4, 2 ),
    ( 5, 0 )
    

    (note I have added a true root element)

    ;with n(CHILD, PARENT, GENERATION, hierarchy) as (
    select CHILD, PARENT,0, CAST(CHILD as nvarchar) as GENERATION from @t
    where PARENT=-1
    union all
    select nplus1.CHILD, nplus1.PARENT, GENERATION+1, 
    cast(n.hierarchy + '.' + CAST(nplus1.child as nvarchar) as nvarchar)
     from 
    @t as nplus1 inner join n on nplus1.PARENT=n.CHILD 
    )
    select CHILD,GENERATION
    from n
    order by hierarchy
    

    returns

    CHILD       GENERATION
    ----------- -----------
    0           0
    2           1
    1           2
    3           3
    4           2
    5           1
    

    Including the hierarchy for illustration:

    CHILD       GENERATION  hierarchy
    ----------- ----------- ------------------------------
    0           0           0
    2           1           0.2
    1           2           0.2.1
    3           3           0.2.1.3
    4           2           0.2.4
    5           1           0.5
    

    Depending on how big your ids get, you might have to do stuff with left-padding with zeroes to get the sorting right.

    Note that SQL 2008 has a built-in hierarchy type for this kind of thing…

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

Sidebar

Related Questions

Using Microsoft SQL Server 2008. I have a table of interest rates. I've manually
I'm using Microsoft SQL Server 2008 Management Studio to create a relational schema by
I am using Microsoft SQL Server. I have a Table which had been updated
I have a table with 300 million rows in Microsoft SQL Server 2008 R2.
I am using Esri ArcMap 10.0 with Microsoft Sql Server 2008 R2 I have
I am using Microsoft SQL Server 2008. I have a stored procedure. Is there
I am using Microsoft SQL Server 2008. I have a very large text file
I am using Microsoft SQL Server and I have a master-detail scenario where I
I'm using VB.NET and Microsoft SQL Server 2005 to create an application. I was
Microsoft SQL Server 2008 (SP1), getting an unexpected 'Conversion failed' error. Not quite sure

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.