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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:34:15+00:00 2026-05-13T10:34:15+00:00

I am trying to write a recursive CTE query in SQL Server 2005 but

  • 0

I am trying to write a recursive CTE query in SQL Server 2005 but am getting an odd set of results. My table is:

PairID  ChildID ParentID
900        1        2       
901        2        3       
902        3        4       

This is my CTE Query:

WITH TESTER (PairID, 
             ChildID, 
             ParentID, 
             Level)
AS (SELECT a.PairID, a.ChildID,a.ParentID, 0 AS Level
    FROM BusinessHierarchy AS a
    UNION ALL
    SELECT b.PairID, b.ChildID, b.ParentID, oh.Level + 1 AS Level
    FROM BusinessHierarchy AS b INNER JOIN
    TESTER AS oh ON b.ChildID =  oh.ParentID)
SELECT 
      x.PairID, 
      x.ChildID,
      x.ParentID,
      x.Level
 FROM TESTER AS x
 ORDER BY x.Level, x.ChildID, x.ParentID

Ok, so I am now getting a dataset return, however, it is not as expected in that it contains repetition in the following manner:

PairID  ChildID ParentID Level
900     1       2        0
901     2       3        0
902     3       4        0
...

900     2       3        1
901     3       4        1
...

900     3       4        2

If someone could explain to me why this is happening and how I would could correct it I would be very grateful.

As far as my last question goes, how would I have to modify it to display the initial childID with each of the parents like this:

Original
PairID  ChildID ParentID Level
900     1       2        0
901     2       3        1
902     3       4        2

I want it displayed as:
PairID  ChildID ParentID Level
900     1       2        0
901     1       3        1
902     1       4        2
  • 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-13T10:34:15+00:00Added an answer on May 13, 2026 at 10:34 am

    Your problem is your starting point. The first part of the CTE query returns all rows (notice their level is all 0).

    Then – Your next part of the query will go and get all the related rows (which adds to your earlier resultset).

    I did notice, however, that you have no record for the id of 4

    if you did, here is what you would do:

    ;WITH TESTER (PairID, ChildID, ParentID, Level) AS (
        SELECT
            a.PairID,
            a.ChildID,
            a.ParentID,
            0 AS Level
        FROM BusinessHierarchy AS a
        LEFT JOIN BusinessHierarchy a2 ON a.ParentID = a2.ChildID
        WHERE a2.PairID is null
    
        UNION ALL
    
        SELECT
            b.PairID,
            b.ChildID,
            b.ParentID,
            oh.Level + 1 AS Level
        FROM BusinessHierarchy AS b
        INNER JOIN TESTER AS oh ON b.ParentID =  oh.ChildID
    )
    
    SELECT 
        x.PairID, 
        x.ChildID,
        x.ParentID,
        x.Level
    FROM TESTER AS x
    ORDER BY x.Level, x.ChildID, x.ParentID
    

    Also, see my answer to a similar question to show the correct sort order (using the path)

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

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • 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 You can use the data in the /proc filesystem to… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer Do you still get the error when you use a… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer You can use array_chunk to create a single array comprised… May 14, 2026 at 3:57 pm

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.