I am trying to write an SQL Procedure that will calculate the Last Price paid for a certain Part.
DECLARE @ItemId int;
SET @ItemId = 1;
SELECT
SD.Price
FROM SalesOrderHeaders AS SH
INNER JOIN SalesOrderDetails AS SD
ON SH.Id = SD.HeaderId
WHERE (SD.LineItemId = @ItemId)
AND (SH.OrderDate = (SELECT
MAX(SH.OrderDate) AS Expr1
FROM SalesOrderHeaders AS SH
INNER JOIN SalesOrderDetails AS SD
ON SH.Id = SD.HeaderId
WHERE (SD.LineItemId = @ItemId))
)
This does produce the results I wanted but, I am doing everything twice. Would someone point me in a better direction?