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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:06:16+00:00 2026-06-03T20:06:16+00:00

This is SQL Server Question I have a set of categories, and their relationship

  • 0

This is SQL Server Question

I have a set of categories, and their relationship results in nested categories.

I want to build a pathway keeping the relationship and build the SEF urls. Here is what I am looking for:

Category table: 
ID, Name
1, Root
2, Cat1
3, Cat2
4, Cat1.1
5, Cat1.2
6, Cat2.1
7, Cat2,2

CategoryChild table: ParentCategoryID, ChildCategoryID
1, 2
1, 3
2, 4
2, 5
3, 6
3, 7

It is an unlimited nested structure. Here is what I am doing (I know its wrong but want something like this):

WITH  MenuItems
    AS (

        SELECT  

        CAST((ItemPath) AS VARCHAR(1000)) AS 'ItemPath',
        CategoryID, Category, ChildID
        FROM    #Mapping
        WHERE   CategoryID = 1
        UNION ALL
        SELECT  
                CAST((items.ItemPath + '-/' + MenuItem.Category) AS VARCHAR(1000)) AS 'ItemPath',
                MenuItem.CategoryID, MenuItem.Category, MenuItem.ChildID
        FROM     #Mapping AS MenuItem
                JOIN MenuItems AS items
                  ON items.ChildID = MenuItem.CategoryID 
       ) 
select * from MenuItems

It gives me something like this:

root--------|1---|root---|2
root--------|1---|root---|3
root/Cat2---|3---|Cat2---|6
root/Cat2---|3---|Cat2---|7
root/Cat1---|2---|Cat1---|4
root/Cat1---|2---|Cat1---|5

So ideally the path should be like this:

root/parent/child (and so on)!

  • 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-03T20:06:17+00:00Added an answer on June 3, 2026 at 8:06 pm

    I’m not sure if this is what you’re looking for but I’ve played with recursive cte’s in the past and so this might be helpful in building the items path.

    NOTE: I’ve included additional information like the Root Id and Level for each item, so that you can change the ordering of the output.

    declare @Category table (Id int, Name varchar(10))
    insert into @Category values (1, 'Root'),(2, 'Cat1'), (3, 'Cat2'), (4, 'Cat1.1'), (5, 'Cat1.2'), (6, 'Cat2.1'), (7, 'Cat2.2')
    
    declare @CategoryChild table (ParentCategoryID int, ChildCategoryID int)
    insert into @CategoryChild values (1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)
    
    ;with cte as 
    (
        -- root part
        select 
            ccParent.ChildCategoryID Id,
            ccParent.ParentCategoryID ParentId,
            c.Name Name,
            CAST(parentCategory.Name + '/' + c.Name as varchar(1000)) as Path,
            ccParent.ChildCategoryID Root,
            0 as Level
        from
            @CategoryChild ccParent
        inner join
            @Category c on c.Id = ccParent.ChildCategoryID
        inner join
            @Category parentCategory on parentCategory.Id = ccParent.ParentCategoryID
        where
            ccParent.ParentCategoryID = 1
    
        union all
    
        -- recursive part
        select
            ccChild.ChildCategoryID Id,
            ccChild.ParentCategoryID ParentId,
            c.Name Name,
            CAST((cte.Path + '/' + c.Name) as varchar(1000)) as Path,
            cte.Root Root,
            cte.Level + 1 as Level
        from
            @CategoryChild ccChild
        inner join
            @Category c on c.Id = ccChild.ChildCategoryID
        inner join
            cte on cte.Id = ccChild.ParentCategoryID
    )
    select cte.Path 
    from cte 
    order by cte.Root, cte.Level
    

    Running the above within my environment gives the following results

    Root/Cat1
    Root/Cat1/Cat1.1
    Root/Cat1/Cat1.2
    Root/Cat2
    Root/Cat2/Cat2.1
    Root/Cat2/Cat2.2
    

    If you were looking to include the Root category in your result set as a standalone item then you can change the first part of the cte to hard code the select of the root item.

    ;with cte as 
    (
        -- root part
        select 
            c.Id Id,
            null ParentId,
            c.Name Name,
            CAST(c.Name as varchar(1000)) as Path,
            c.Id Root,
            0 as Level
        from
            @Category c
        where 
            c.Name = 'Root'
    
        union all
    
        ... same as before
    

    Giving the follow

    Root
    Root/Cat1
    Root/Cat1/Cat1.1
    Root/Cat1/Cat1.2
    Root/Cat2
    Root/Cat2/Cat2.1
    Root/Cat2/Cat2.2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this question in which I have a SQL Server Compact Edition database
Extreme newbie question. I have my database (SQL Server) set up to cascade deletes
Warm Standby SQL Server/Web Server This question might fall into the IT category but
We are using SQL Server 2005, but this question can be for any RDBMS
I am using the SQL Server PHP Driver, I think this question can be
My question is similar to this MySQL question, but intended for SQL Server: Is
This SQL Server 2005 T-SQL code: DECLARE @Test1 varchar; SET @Test1 = 'dog'; DECLARE
I want to port this SQL query, which works just fine on SQL Server,
I have a table like this in SQL Server: varID(PK) dataID(PK) is_used A 1
Using SQL Server 2008. This is a really junior question and I could really

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.