Solved

SQL query

Posted on 2011-02-24
6
477 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]
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

803 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