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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:43:03+00:00 2026-06-17T08:43:03+00:00

I have the following Location table ID Name ParentID ———————– 1 TopLevel NULL 2

  • 0

I have the following Location table

ID  Name       ParentID
-----------------------
1   TopLevel   NULL
2   Region 1   1
3   Fleet 1    2
4   Fleet 2    2

I have just recently finished up writing a recursive CTE to display this structure in a hierarchy approach:

TopLevel
-Region 1
--Fleet 1
--Fleet 2

I now need to write a query to return this structure in a column hierarchal format:

Level1     Level2   Level3  Level4  Level5  Level6
Top Level
Top Level  Region 1 Fleet 1
Top Level  REgion 1 Fleet 2

The maximum levels deep a location can go is 9. How would I accomplish this? I was trying to use the recursive CTE and build on that, but I don’t think that is going to work. Any other ideas?

  • 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-17T08:43:03+00:00Added an answer on June 17, 2026 at 8:43 am

    Here would be one method to accomplish the output, using a recursive CTE hard-coded to fill out up to 9 levels worth of hierarchy (you would need to change table/column references as necessary to your real object names, and you may need to increase varchar(50) if the actual type of name column is wider than that):

    with cte as (
      select id, 1 as level,
        name as level1,
        cast(null as varchar(50)) as level2,
        cast(null as varchar(50)) as level3,
        cast(null as varchar(50)) as level4,
        cast(null as varchar(50)) as level5,
        cast(null as varchar(50)) as level6,
        cast(null as varchar(50)) as level7,
        cast(null as varchar(50)) as level8,
        cast(null as varchar(50)) as level9
      from table1 t
      where parentid is null
    
      union all
    
      select t.id, cte.level + 1 as level,
        case when level = 0 then t.name else cte.level1 end as level1,
        case when level = 1 then t.name else cte.level2 end as level2,
        case when level = 2 then t.name else cte.level3 end as level3,
        case when level = 3 then t.name else cte.level4 end as level4,
        case when level = 4 then t.name else cte.level5 end as level5,
        case when level = 5 then t.name else cte.level6 end as level6,
        case when level = 6 then t.name else cte.level7 end as level7,
        case when level = 7 then t.name else cte.level8 end as level8,
        case when level = 8 then t.name else cte.level9 end as level9
      from table1 t
      inner join cte on cte.id = t.parentid
    )
    select * from cte;
    

    Sample Output:

    | ID | LEVEL |   LEVEL1 |   LEVEL2 |  LEVEL3 | LEVEL4 | LEVEL5 | LEVEL6 | LEVEL7 | LEVEL8 | LEVEL9 |
    ----------------------------------------------------------------------------------------------------
    |  1 |     1 | TopLevel |   (null) |  (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    |  2 |     2 | TopLevel | Region 1 |  (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    |  3 |     3 | TopLevel | Region 1 | Fleet 1 | (null) | (null) | (null) | (null) | (null) | (null) |
    |  4 |     3 | TopLevel | Region 1 | Fleet 2 | (null) | (null) | (null) | (null) | (null) | (null) |
    

    Demo: http://www.sqlfiddle.com/#!6/0bd5d/14

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

Sidebar

Related Questions

i have the following table structure table location has - id - parentLocation_id (relation
I have the following code showing location objects from an NSArray in a table,
I have the following database design: Users Table: NetworkID, Name, BadgeNo, DepartmentCode Events Table:
I have a table with following structure, +-----------------------+------------------+------+-----+---------+----------------+ | Field | Type | Null
hey, following problem here: i have table with persons that have one location, if
I have the following query, and it produces a table. SELECT c.name AS campaign
I have the following DB Schema :- Data is ... Location Table 1. New
Suppose I have the following table house: House: id name cityID Where cityID refers
Suppose i have the following table book ----- id name genre check_out_date location_id Can
I have the following in nginx: # backend location /backend/ { deny all; }

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.