I am not that familiar with the ins and outs of SQL queries, but I can usually get things done with a stored procedure, but I have decided this is one time where I absolutely know what I am doing can be done in just one simple query, I am just not sure what I need to do next.
My Stored Proc queries my Sales table using the @input_order_id variable so I can get the two fields (@shipping_address_id, @buyer_id) for that particular order_id that was input. Then I do another select on the SAME sales table and use these two fields in my WHERE clause. The reason I do this is because if a Buyer orders two separate orders, they will have two different order_ids but they should essentially be grouped together and shipped all together at the end of the day. So if two different order_id's have the same shipping_address_id and buyer_id then they ship together and essentially I need to pull these together.
I know I can do all of this comparing in one query, but everything I try is giving me the wrong results.
The Stored Procedure below exactly accomplishes what I want to do, but I'd much rather just use 1 simple query instead.
Essentially I need all of the PartNums for an order_id.
Sales Table contains:
OrderID, ItemID
Items Table contains:
ItemID, IventoryID
Iventory Table contains:
InventoryID, PartNum <--- That's the one I want
Any ideas to help me condense this down to just one query?
My Stored Proc queries my Sales table using the @input_order_id variable so I can get the two fields (@shipping_address_id, @buyer_id) for that particular order_id that was input. Then I do another select on the SAME sales table and use these two fields in my WHERE clause. The reason I do this is because if a Buyer orders two separate orders, they will have two different order_ids but they should essentially be grouped together and shipped all together at the end of the day. So if two different order_id's have the same shipping_address_id and buyer_id then they ship together and essentially I need to pull these together.
I know I can do all of this comparing in one query, but everything I try is giving me the wrong results.
The Stored Procedure below exactly accomplishes what I want to do, but I'd much rather just use 1 simple query instead.
Code:
ALTER PROCEDURE [dbo].[Get_PartsToShip]
@input_order_id nvarchar(25)
AS
BEGIN
DECLARE @shipping_address_id int
DECLARE @buyer_id int
SET @shipping_address_id = (SELECT ShippingAddressID FROM Sales WHERE Sales.OrderID = @input_order_id)
SET @buyer_id = (SELECT BuyerID FROM Sales WHERE Sales.OrderID = @input_order_id)
SELECT S.OrderID, S.BuyerID, S.ShippingAddressID, I.ItemID, INV.InventoryID, INV.PartNum
FROM Sales AS S
INNER JOIN Items I ON I.ItemID = S.ItemID
INNER JOIN Inventory INV ON INV.InventoryID = I.InventoryID
WHERE BuyerID = @buyer_id AND ShippingAddressID = @shipping_address_id
END
Essentially I need all of the PartNums for an order_id.
Sales Table contains:
OrderID, ItemID
Items Table contains:
ItemID, IventoryID
Iventory Table contains:
InventoryID, PartNum <--- That's the one I want
Any ideas to help me condense this down to just one query?