Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting columns from multiple tables

Status
Not open for further replies.

10basetom

Technical User
Dec 9, 2006
2
US
Hello,

Given the following tables:

Code:
SKU Table
OID NAME
=== ================
1   IBM ThinkPad T23
2   Dell GX150
3   Compaq Evo D510

CAT Table (PARENT_OID->CAT.OID)
OID NAME      PARENT_OID
=== ========= ==========
1   Computers NULL
2   Notebooks 1
3   Desktops  1

MAP Table (OID->SKU.OID, PARENT_OID->CAT.OID)
OID PARENT_OID
=== ==========
1   2
2   3
3   3

I'm trying to write a SQL query that returns this result set:

Code:
PRODUCT NAME     CATEGORY  PARENT CATEGORY
================ ========= ===============
IBM ThinkPad T23 Notebooks Computers
Dell GX150       Desktops  Computers
Compaq Evo D510  Desktops  Computers

I know how to join all three tables to return the first two columns, but I'm struggling to use the PARENT_OID value from the CAT table to retrieve the category name of that category's parent category and then return it in the same result set. Any ideas?

TIA,
Tom
 
Something like this ?
SELECT S.Name, C.Name, P.Name
FROM SKU S
INNER JOIN MAP M ON S.OID=M.OID
INNER JOIN CAT C ON M.Parent_OID=C.OID
INNER JOIN CAT P ON C.Parent_OID=P.OID

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
hi PHV,

thanks so much for leading me to the light! i had a doh! moment as soon as i read your response -- i didn't realize that you could join the same table twice under different names. learning is so much fun.

thanks again,
tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top