Solved

SQL query

Posted on 2011-02-24
6
457 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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
Comment Utility
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
Comment Utility
rick:

This can be multi level categories each with a categoryname.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 40

Expert Comment

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

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
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
Comment Utility
TY
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now