MS SQL - How do I fetch the same column twice from one table?

Hi,

I have two tables. One has a full list of IDs with their descriptions, the other lists the relationship between the IDs. I realise this sounds a little cryptic so I'll try and illustrate it in the code section.

What I need to do is have a full list of IDs with their parent and child descriptions...see desired result table in the coding section.
Table 1:
ID     Descr
1      James
2      Philip
3      Peter
4      Allan
 
Table 2:
ID     ChildID
1      2
1      3
2      NULL
3      4
4      NULL
 
Desired Result:
ID    ParentDescr     ChildDescr
1     James               Philip
1     James               Peter
2     Philip                 NULL
3     Peter                 Allan
4     Allan                 NULL

Open in new window

LVL 2
joshgeakeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
Something like this should work:
select p.ID, p.Descr AS ParentDescr, c.Descr AS ChildDescr
from Table1 p
inner join Table2 pc ON pc.ID = p.ID
left join Table1 c ON pc.ChildID = c.ID

Open in new window

0
joshgeakeAuthor Commented:
thanks, my issue was how to query the same table twice for the descriptions of the child and parent IDs but it looks like this sorts it - i'll comment tomorrow when i'm back in the office.

thanks again!
0
pssandhuCommented:
Select c.ID, C.Desc ParentDescr, m.ChildID, m.Descr as ChildDescr
From Table1 c INNER JOIN (
Select a.ID, a.ChildID, t.Descr
From  Table1 t INNER JOIN  Table2 d
           ON t.ID = ChildID
Where ChildID IS NOT NULL) as m
ON c.ID = m.Id
0
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

pssandhuCommented:
I am really slow today... :-)
0
Racim BOUDJAKDJIDatabase Architect - Dba - Data ScientistCommented:

Alternatively...Something like this should work...
select ALL.*
from
(
select A.id, A.Descr as ParentDescr, C.Descr as ChildDescr from table1 A inner join table2 B on  A.id = B.id inner join table1 C on B.ChildID = C.id where B.ChildID is not null
union all
select A.id, A.Descr, NULL from table1 A inner join table2 B on  A.id = B.id where B.ChildID is null
) ALL
order by ALL.id

HTH
0
joshgeakeAuthor Commented:
Right i'm still a little stuck, see the code snippet...

It looks like its listing all part groups for every product when what I want it to do is only list its relevent group. Each part can be in a number of product groups but it's usually 1. Thus it will then list the child group.

Any ideas?
SELECT     epar.part AS PartCode, p.prod_group, c.prod_group AS child_group, p.descr AS ParentDescr, c.descr AS ChildDescr
FROM         epar LEFT OUTER JOIN
                      wgrp_part ON epar.part = wgrp_part.part INNER JOIN
                      wgrp AS p ON wgrp_part.prod_group = p.prod_group INNER JOIN
                      wgrp_sgrp AS pc ON pc.Prod_group = p.prod_group LEFT OUTER JOIN
                      wgrp AS c ON pc.sub_group = c.prod_group
WHERE     (c.descr IS NOT NULL)
ORDER BY epar.part

Open in new window

0
joshgeakeAuthor Commented:
by the way...
epar - lists all parts.
wgrp_part - lists all parts and their prod_groups.
wgrp_sgrp - lists all sub_groups and their respective prod_group.
wgrp - lists all prod_groups and sub_groups descriptions.
0
pssandhuCommented:
Sorry, but I am a little confused. Can you please post some sample data from the tables we can work with?
P.
0
Kevin CrossChief Technology OfficerCommented:
The way (including table structure) the question was asked, it was answered here - http:#24765175.

However, the asker changed things up here - http:#24771302 for which additional information was requested here - http:#24772162 that was never responded to; therefore, would recommend "delete - no refund" unless my original answer will be honored.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pssandhuCommented:
I would recommend accepting mvisa's answer since it was answered based on what was requested.
P.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.