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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:16:23+00:00 2026-05-25T10:16:23+00:00

I have a table with columns and data as follows: Table1 ID Name PID

  • 0

I have a table with columns and data as follows:

Table1
ID  Name    PID
A1  Apple   P1
B1  Book    A1
B2  Brook   A1
C1  Cat     B1
C2  Cook    B1
C3  Car     B1
D1  Dog     B2
D2  Doll    B2
E1  Egg     C1

I want the results as follows:

ID  Name                    Depth
B1  Apple\Book              2
C1  Apple\Book\Cat          3
E1  Apple\Book\Cat\Egg      4
C2  Apple\Book\Cook         3
C3  Apple\Book\Car          3
B2  Apple\Brook             2
D1  Apple\Brook\Dog         3
D2  Apple\Brook\Doll        3

The relationship is that a row is a child of another row if PID of that row is equal to ID of the parent row.

Apple is the base. So the first statement would be something like:

Select ID, Name, 2 from Table1 where PID=(select ID from Table1 where Name='Apple');

My current solution is creating a lot of views and save all those similar statements like that to views and union them together. But I don’t want that. I want to finish that within 1 select statement.A

  • 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-25T10:16:23+00:00Added an answer on May 25, 2026 at 10:16 am
    declare @Table1 table
    (
      ID varchar(2),
      Name varchar(10),
      PID varchar(2)
    )
    
    insert into @Table1 values  
    ('A1',  'Apple',   'P1'),
    ('B1',  'Book',    'A1'),
    ('B2',  'Brook',   'A1'),
    ('C1',  'Cat',     'B1'),
    ('C2',  'Cook',    'B1'),
    ('C3',  'Car',     'B1'),
    ('D1',  'Dog',     'B2'),
    ('D2',  'Doll',    'B2'),
    ('E1',  'Egg',     'C1')
    
    ;with C as
    (
      select T.ID,
             cast(T.Name as varchar(max)) as Name,
             1 as Depth
      from @Table1 as T
      where T.Name = 'Apple'
      union all
      select T.ID,
             cast(C.Name+'\'+T.Name as varchar(max)),
             C.Depth + 1
      from @Table1 as T
        inner join C
          on T.PID = C.ID
    )
    select C.ID,
           C.Name,
           C.Depth
    from C
    where C.Depth > 1
    order by C.Name
    

    Edit With out the Apple.

    ;with C as
    (
      select T.ID,
             cast(T.Name as varchar(max)) as Name,
             1 as Depth
      from @Table1 as T
        inner join @Table1 as TP
          on T.PID = TP.ID
      where TP.Name = 'Apple'
      union all
      select T.ID,
             cast(C.Name+'\'+T.Name as varchar(max)),
             C.Depth + 1
      from @Table1 as T
        inner join C
          on T.PID = C.ID
    )
    select C.ID,
           C.Name,
           C.Depth
    from C
    order by C.Name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table in data base name train delay, with columns train number(int), DelayTime(int),
The table columns have the data type BLOB and CLOB. What are the corresponding
I have some data I am querying. The table is composed of two columns
i done as follows i have a method with brings data of particular table,
I have a table table_a with a column containing data as follows: .aaa01932 .asd02343
My scenario is as follows: I have a table of data (handful of fields,
I have a collection of data that looks as follows: id name c1 c2
If I have a table column with data and create an index on this
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a table of data that I need to dynamically add a column

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.