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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:25:57+00:00 2026-05-29T04:25:57+00:00

I have a table that looks like this: ID |Name |Parent 1 |A |NULL

  • 0

I have a table that looks like this:

ID     |Name    |Parent
1      |A       |NULL
2      |B       |NULL
3      |C       |1
4      |D       |1
5      |E       |3
6      |F       |5

I would like to have a view return this:

ID     |Name    |ParentNames
1      |A       |
2      |B       |
3      |C       |A
4      |D       |A
5      |E       |A > C
6      |F       |A > C > E

I tried to left join a view showing an IDs first parent and left joining it with itself but that didn’t work.

Is there a way to do this without a stored procedure/function? I’ve got ~15k rows with ~0-5 parents each but I’d rather not hard code a maximum of 5 parents.

  • 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-29T04:25:58+00:00Added an answer on May 29, 2026 at 4:25 am

    You can use a recursive CTE.

    declare @T table(ID int, Name char(1), Parent int);
    
    insert into @T values  
    (1      ,'A'       ,NULL),
    (2      ,'B'       ,NULL),
    (3      ,'C'       ,1),
    (4      ,'D'       ,1),
    (5      ,'E'       ,3),
    (6      ,'F'       ,5);
    
    with C as
    (
      select ID,
             Name,
             Parent,
             cast('' as varchar(max)) as ParentNames
      from @T
      where parent is null
      union all
      select T.ID,
             T.Name,
             T.Parent,
             C.ParentNames + ' > ' + C.Name
      from @T as T         
        inner join C
          on C.ID = T.Parent
    )      
    select ID,
           Name,
           stuff(ParentNames, 1, 3, '') as ParentNames
    from C;     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a categories table that looks like this: id | name | parent
I have a table that looks like this: Id | Name | Parent ---+-------------------------+-------
I have a database that looks like this: ID parent name description _record_status _log_user
I have a table that looks like this: id datatype name value 0 nvarchar(255)
I have a table that looks like this: category category_id name category_seo_friendly_url left_id right_id
So I have a table that looks like this: Name ID TaskID HoursAssigned ----------------------------------------------------------------
My parent-child table looks like this: ID, ParentID, Name, Population 1, Null, Asia, 40
I have one table that looks something like this: id parent_id name 1 0
I have a table that looks a bit like this actors(forename, surname, stage_name); I
I have a table that looks like this: nid vid tid 1 2 3

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.