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

  • Home
  • SEARCH
  • 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 7596399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:54:33+00:00 2026-05-30T21:54:33+00:00

I have a parents table looks like this CHILD_ID | PARENT_ID | NAME 1

  • 0

I have a parents table looks like this

CHILD_ID | PARENT_ID | NAME
1        | Null      | Bill
2        | 1         | Jane
3        | 1         | Steve
4        | 2         | Ben
5        | 3         | Andrew

Id like to get a result set like this

Bill
---Jane
------Ben
---Steve
------Andrew

I know I need to do a rank query to rank the levels and a self join but all I can find on the net is CTE recursion

I have done this in Oracle before but not in MS SQL

  • 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-30T21:54:35+00:00Added an answer on May 30, 2026 at 9:54 pm

    Bit hacky and can be improved but hopefully it shows the principle…

    ;with relation (childId, parentId, childName, [level], [orderSequence])  
    as  
    (  
    select childId, parentId, childName, 0, cast(childId as varchar(20))  
    from @parents  
    where parentId is null  
    union all  
    select p.childId, p.parentId, r.[level]+1, cast(r.orderSequence + '_' + cast(p.childId as varchar) as varchar(20))  
    from @parents p  
    inner join relation r on p.parentId = r.childId  
    )  
    
    select right('----------', ([level]*3)) +childName  
    from relation  
    order by orderSequence
    

    If however you want to avoid recursion then an alternative approach is to implement a tree table with the relevant tree structure information – see http://www.sqlteam.com/article/more-trees-hierarchies-in-sql for a walk through

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

Sidebar

Related Questions

I have one table that looks something like this: id parent_id name 1 0
I have a table structure like this: id name parent_id 1 root_category Null 2
I have a table that looks like this: Id | Name | Parent ---+-------------------------+-------
My parent-child table looks like this: ID, ParentID, Name, Population 1, Null, Asia, 40
I have this table. id name parents 1 Apparel 46 Apparel 1 47 Child
I have a mysql table named category. the basic structure looks like this- ------
I have a table in SQL Server that is structured like this: id Name
I have a BUSINESS table that looks like this: BUSINESS_ID | BRN | CODE
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
I've got a table that stores inventory for a store that looks like this

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.