Solved

Query to bring up parent child relationships

Posted on 2013-01-11
4
920 Views
Last Modified: 2013-01-11
Hi,

I have a set of data that contains the ID that links a child node to a parent.  But the issue is that there are multiple levels and I need a way to query all the levels.

For example, the data set is as follows:
Child ID      Child Name      Parent ID       Parent Name
30      AAA      20      AA
31      BBB      21      BB
32      CCC      22      CC
33      DDD      23      DD
34      EEE      24      EE
35      FFF      25      FF
20      AA      10      A
21      BB      11      B
22      CC      12      C
23      DD      13      D
24      EE      14      E
25      FF      15      F
10      A            
11      B            
12      C            
13      D            
14      E            
15      F            

I need a query that I can use to get the following results:
Child ID      Child Name      Parent 0 ID      Parent 0 Name      Parent 1 ID      Parent 1 Name
30      AAA      20      AA      10      A
31      BBB      21      BB            
32      CCC      22      CC            
.........

Basically I need to show all the parents of each child ID that leads to the parent ID. Any suggestions on how I can query this? Thanks.
0
Comment
Question by:iamnamja
[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
  • 2
  • 2
4 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38767591
assuming your table name is Child


SELECT Child.[Child ID], Child.[Child Name], Child_1.[Parent ID] AS [Parent 0 ID], Child_1.[Parent Name] AS [Parent 0 Name], Child_2.[Parent ID] AS [Parent 1 ID], Child_2.[Parent Name] AS [Parent 1 Name]
FROM (Child LEFT JOIN Child AS Child_1 ON Child.[Parent ID] = Child_1.[Parent ID]) LEFT JOIN Child AS Child_2 ON Child_1.[Parent ID] = Child_2.[Child ID];


you can extend the query by adding copy of the child table to the query and create a left join of the fields Child Id and Parent ID
0
 

Author Comment

by:iamnamja
ID: 38767612
So the only logical method is to extend to query to a possible number of parent child legs?  If there are 10 parent/child hierarchy, then we need to extend to 10?
I was hoping for a simpler solution as there may be more than 10, 20, etc...
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 38767645
<So the only logical method is to extend to query to a possible number of parent child legs?>
Yes, by using a similar query i posted above

but, i think to make it dynamic, you will need to use recordsets and vba codes .
0
 

Author Comment

by:iamnamja
ID: 38769248
got it.  I ended up using VBA code to get this done.
I was hoping there would be a "simpler" method.  Thank you
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

624 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