Solved

SQL query

Posted on 2011-02-24
6
483 Views
Last Modified: 2012-08-14
I am having a problem writing a SQL query...

I have a table named Categories with the following fields:

CategoryID
CategoryName
ParentCategoryID

I need to be able to pass the Category ID a retrieve it's path in the following format

example:

Vehicle/Car/Sedan

Where Vehicle is the top level category and Sedan is the bottom level with the category ID that was passed. ParentCategoryID establishes the relationships between the various categories.

0
Comment
Question by:vbnetcoder
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34970276
please check this article:
http://msdn.microsoft.com/en-us/magazine/cc794278.aspx

you can see how to create such a query, you could either go through it entirely, or start around Figure 5, where you can  see the results of the query just above, using WITH syntax which can do exactly what you need
0
 
LVL 13

Expert Comment

by:Rick
ID: 34970362
I'm not sure I understood the question.
Could you not do something like this:

Select CategoryID + '/' + CategoryName + '/' + ParentCategoryID [CategoryID]
From Categories
Where ...
0
 

Author Comment

by:vbnetcoder
ID: 34970384
rick:

This can be multi level categories each with a categoryname.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 40

Expert Comment

by:Sharath
ID: 34972439
Can you post some sample data from your table.
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 34972498
If you have data like in my sample, and looking for a tree structure from parent to child, you can try CTE.
declare @Categories table(CategoryID int,CategoryName varchar(100),ParentCategoryID int)
insert @Categories values (1,'Vehicle',null)
insert @Categories values (2,'Car',1)
insert @Categories values (3,'Sedan',2)

select * from @Categories
/*
CategoryID	CategoryName	ParentCategoryID
1	Vehicle	NULL
2	Car	1
3	Sedan	2
*/
;WITH CTE AS(
SELECT CategoryID, CategoryName, ParentCategoryID, convert(varchar(max),CategoryName) Tree
  FROM @Categories
 WHERE ParentCategoryID is null
 UNION ALL
SELECT c.CategoryID, c.CategoryName, c.ParentCategoryID, p.Tree + ' / ' + convert(varchar(max),c.CategoryName)
  FROM @Categories c
  JOIN CTE p
    ON c.ParentCategoryID = p.CategoryID
)
SELECT * FROM CTE where CategoryName = 'Sedan' 
/*
CategoryID	CategoryName	ParentCategoryID	Tree
3	Sedan	2	Vehicle / Car / Sedan
*/

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 35098342
TY
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question