Thursday 15 January 2015

sql server - Get Cat1/Cat2/Cat3 string from self referencing table -


I am trying to generate a string that is similar to Cat1 / Cat2 / Cat3 etc. from a self-referenced table Reads. / P>

The data looks like this:

Category Volume

Category ID, [Name], Parent ID

1, root, 0

2, cat 1, 1

3, cat 2, 2

4, cat 3, 3

5, cat 4, 1

6, cat 5, 5

I want to create:

root / cat 1 / cat2 / cat3 root / cat 4 / cat 5

How can I do this in SQL Server

You can use CTE

 with  CTA (select CD, CAST (naming as naming) (4000) Name in the name where the parent = 0 union Select all c.CategoryID, CAST (e.Name + '/' + c.Name AS VARCHAR (4000)) from CTE in these inner section C. ParentID = E. Caribbean C Select c1.Name from CTC1 where it is not present (selection 1 to CTC2 where C 1.name    

No comments:

Post a Comment