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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:11:58+00:00 2026-06-15T05:11:58+00:00

I am using SQL Server 2008 and have a SQL challenge I have never

  • 0

I am using SQL Server 2008 and have a SQL challenge I have never run into before. Consider the following table that represents a hierarchical category list:

ID  Name            Description                     ParentID
--- --------------- ------------------------------- --------
1   Bicycle         Bicycles and Tricycles          {null}
2   Wheels          Wheels                          1
3   Spoked          Spoked Wheels                   2
4   Skate Boards    Skate Boards and accessories    {null}
5   Wheels          Skate Board Wheels              4
6   Polyurethane    Polyurethane Wheels             5

Results I am looking for:

ID  Heirarchy                               Description
--- --------------------------------------- ------------------------------------
1   Bicycle                                 Bicycles and Tricycles
2   Bicycle, Wheels                         Wheels
3   Bicycle, Wheels, Spoked                 Spoked Wheels
4   Skate Boards                            Skate Boards and accessories
5   Skate Boards, Wheels                    Skate Board Wheels
6   Skate Boards, Wheels, Polyurethane      Polyurethane Wheels

I would like to Query this table and return a name for each row that would represent the hierarchy by concatenating the name(s) of each parent to the child. The hierarchy does not have a pre-set nesting depth and I would like to be able to run this in a single query. How can this be accomplished?

  • 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-06-15T05:12:00+00:00Added an answer on June 15, 2026 at 5:12 am
    with tree as (
       select id, 
              cast(name as varchar(max)) as hierarchy,
              name, 
              description
       from the_table
       where parentID is null
       union all
       select c.id, 
              p.hierarchy + ', ' + c.name,
              c.name,
              c.description
        from the_table c
           join tree p on p.id = c.parentID
    ) 
    select * 
    from tree;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008 and I have a table that has a 'Status'
I am using SQL Server 2008 R2 and have run into an issue with
I'm using sql server 2005/2008 and I have a table that it's PK (rec_index)
I am using SQL Server 2008. I have a table (TBL_FILE) that stores user
I am using SQL Server 2008 . I have a table with the following
I'm using SQL Server 2008 With this table : and i have the following
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)
Using Microsoft SQL server 2008 I have a query that is in need of
I'm using SQL Server 2008 and I have a table with three columns: Length
I am using T/SQL in SQL Server 2008 I have a table MyTable with

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.