Solved

Query information from single table with parent-child relationship

Posted on 2012-03-21
3
153 Views
Last Modified: 2014-06-03
I have a fairly simple database that is giving me issues.  The main table (tblCategory) is structured as such:

ID    Category    ParentCategoryID
1          Parent        NULL
2          Child           1

Ideally when I execute the query I would get the following results:
ID    Category    ParentCategoryID   ParentCategory
1          Parent        NULL                      NULL
2          Child           1                            Parent

This is the SQL query I wrote to try to pull the information:

SELECT ID, Category, ParentCategoryID, (SELECT Category FROM tblCategory WHERE tblCategory.ParentCategoryID = tblCategory.ID) AS ParentCategory
FROM tblCategory;

I know I could use two tables to do this; however the data structure for the both the parent and child recordsets are identicle.  I also like the idea of a recursive relationship data structure that could allow for infinite levels without having to create a table each time a new level of data is added.  

I appreciate your help and feedback.
0
Comment
Question by:Bushmouse
3 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37747420
Read this URL to know more about how to access Data from Table having recursive information:-

http://msdn.microsoft.com/en-us/library/ms186243.aspx

The term to query is Common Table Expression and is completely related to SQL Server 2005
0
 
LVL 22

Expert Comment

by:Steve Wales
ID: 40107947
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

920 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

15 Experts available now in Live!

Get 1:1 Help Now