[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Get all parents from sql query

Posted on 2010-08-26
6
Medium Priority
?
423 Views
Last Modified: 2012-05-10
Hi,

I have a table looking like this:
categoryId     category     parentID
0                    Home          Null
1                    furniture     0
2                    Cars           0
3                    Chair          1
4                    Volvo         2

Now I want to be able to select the hierarchy based on the categoryID, so that if the categoryID for example is 4 then the result of the query would be:
categoryID        category     parentID
0                       Home          Null
2                       Cars            0
4                       Volvo          2

How can I achieve this?

Thanks for help!

Peter
0
Comment
Question by:peternordberg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 8

Expert Comment

by:kingjely
ID: 33530869
another example might help but,

select CategoryID, category, parentID
From YourTable
order By categoryID asc

Yea nah, i don't see what your trying to do at all..
0
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33530892
Please check the following thread:
http://www.vbforums.com/showthread.php?t=366078
0
 

Author Comment

by:peternordberg
ID: 33530893
I want the selected hiearchy based on what categoryID is chosen, like a sitepath (breadcrunch)

Peter
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:Jini Jose
ID: 33530926
you can use hierarchycte to get all parents of a child and all childs and sub childs of a parent.

here is the sample and usage.

http://www.sqlservercentral.com/Forums/Topic796015-338-1.aspx
0
 
LVL 8

Accepted Solution

by:
rushShah earned 2000 total points
ID: 33530960
hi if you are using sql server 2005 or above than try this..


;WITH CTE AS
(
	SELECT	categoryId, category, parentID
	FROM	yourtable
	WHERE	categoryId=4
	UNION ALL
	SELECT	t1.categoryId, t1.category, t1.parentID
	FROM	yourtable t1
	INNER JOIN	CTE c ON c.ParentID=t1.CategoryID
)

SELECT	* FROM CTE ORDER BY CategoryID

Open in new window

0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 33531230
Nice elegant solution, RushShah! Congrats :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

650 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