I want to create a query statement for MYSQL which will give me the most recent price of an item. A table contains a bunch of prices (UnitPrice
) paired with each item (MIL_id)
. However, I want the most recent price (UnitPrice
) for each item (MIL_id
). Each row also has a ReqNum
column which increments for each entry and a Date
column for each entry. So how can I make a query which will give me the most recent price (UnitPrice
) with the newest Date
or ReqNum
?
What I have so far:
SELECT t1.VendorPtNum, t1.UnitPrice, MAX(t1.Date), MAX(t1.ReqNum), t2.MIL_id
FROM tblMatInvList t2
LEFT JOIN tblPurchaseReq t1 ON t1.VendorPtNum = t2.VendPtNum
GROUP BY t2.MIL_id
HAVING MAX(t1.Date)
So, you have a table
tblMatInvList
--------------------+
VendorPtNum | MIL_id
--------------------+
and a table
tblPurchaseReq
---------------------------------------+
VendorPtNum | UnitPrice | Date | ReqNum
---------------------------------------+
I think you can use something like this
SELECT * FROM tblMatInvList
INNER JOIN
(SELECT MAX(Date) as date,
VendorPtNum
FROM tblPurchaseReq
GROUP BY tblPurchaseReq.VendorPtNum) prices
ON prices.VendorPtNum = tblMatInvList.VendorPtNum;