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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drilling Down through Mulitple Tables w/ 1 Efficiency..

Status
Not open for further replies.

zyman10

Technical User
Dec 7, 2008
41
US
Hi All,

I have a question that is probably pretty easy, so bear with me here... All I want to do is take an OrderNum I am given and return all of the ProductTitle(s). Obviously all of the tables below have more fields, but I have only listed the important ones. After the table structure, I explain what I currently do.

My DB Table Structure:

Code:
Orders:
OrderID
OrderNum (This is the field I am bringing into the StoredProc -- It can appear multiple times in the table)
ItemID

Items:
ItemID
InventoryID

Inventory:
InventoryID
ProductID

Products:
ProductID
ProductTitle

Since OrderNum can appear multiple times in the table, it is NOT the primary key for OrdersProcessing, but this is how I want it, because an Order may contain more than one Item (ItemID). So since there may be more than a 1 to 1 relationship at the top, my Stored Procedure creates a TempTable for every level of the drill down to ProductTitle. For the first level:

1.) I create a table called @ItemsFound
2.) I do a select on the Orders Table to find all of the ItemIDs that match that OrderNum (that may appear more than once, possibly giving us multiple ItemIDs at this point)
3.) Insert those ItemIDs into the @ItemsFound table.
4.) Create another tempTable called @InventoryFound.
5.) WHILE loop through the @ItemsFound table stopping at each ItemID and getting the correct InventoryID and INSERTING it into @InventoryFound.

Then I repeat that whole process for the next step until I'm all the way down to ProductTitle, and then I have a list of the ProductTitles I want for the OrderNum that may have contained more than one Item...

Isn't there a way to do this more efficiently with less creating of temp tables. Maybe even a single query? I don't need the whole query written or anything, just the logic behind how to do maybe one step of the way, and then I can run with it.

I'll be happy to provide more detail if this isn't clear enough. Thanks for any help.
 
Code:
SELECT *
       FROM Orders
INNER JOIN Items     ON Items.ItemId       = Orders.ItemId
INNER JOIN Inventory ON Inventory.ItemId   = Items.InventoryID
INNER JOIN Products  ON Products.ProductID = Inventory.ProductID
WHERE Orders.OrderNum = @Something

NOT TESTED!!!!
The order of tables in JOIN section is important.


Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Boris

wow neat man. so this will work even if there is more than one itemID for a orderNum?

So like:

OrderNum --- ItemID
29532 --- 543
29532 --- 775
29532 --- 658
29532 --- 198

It will - theoretically - go through and get all of the ProductTitles for all those ItemIDs? Or just the first one?

Thanks dude. Didn't realize JOINING was so useful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top