G
Guest
I am pretty inexperienced, & until now have been using Last() in a group by query to return the most recent equipment transfer (and therefore current location) for each piece of equipment in my database. When I began noticing unexpected query results and did a little research, it became apparent that Last() is not the way to go, so I've been experimenting with Max([ShipDate]) but I'm having a problem with this now. I have two tables involved in tracking transfers. tblEquipTransfer, the "parent" table, has a primary key [TransId] and main fields [ShipDate] & [JobNo]. tblEquipTrans, the "child" table, has the foreign keys [EquipId] (looked up from Equipment table) and [TransId] which is linked to the main record in tblEquipTransfer. I joined the two tables and have no problem obtaining the most recent [ShipDate] for each [EquipId], but I need the primary key [TransId] in my results in order to pull in all the other applicable fields. I can't use Max for [TransId], because transfers are occasionally entered out of date sequence, so Max([ShipDate]) and Max([TransId]) can each return a value from a different row. I tried joining [EquipId] and [ShipDate] to get [TransId], but we can have equipment transferred more than once on a given date. (At least when I was using Last() I got a distinct row, even though it wasn't always for the most recent date or TransId.) Any suggestions would be welcome!
THIS IS THE QUERY I WAS USING:
SELECT Last(EquipTransfer.ShipDate) AS LastOfShipDate, Last(EquipTrans.TransId) AS LastOfTransId, Last(EquipTransfer.JobNo) AS LastOfJobNo, Equipment.EquipId, Equipment.EquipType, Equipment.SerialNo, Last(EquipTrans.ToMaster) AS LastOfToMaster, Last(EquipTrans.Comments) AS LastOfComments, EquipTrans.SlctEqpId, Last(EqpTrnsVndr.VendorId) AS LastOfVendorId, Last(EquipTrans.RMA) AS LastOfRMA, Last(EqpTrnsVndr.Mas)
AS LastOfMas, Last(qlkpJobNo.Manager) AS LastOfManager, Last(qlkpJobNo.MajorLoc) AS LastOfMajorLoc
FROM ((EquipTransfer INNER JOIN qlkpJobNo ON EquipTransfer.JobNo = qlkpJobNo.JobNo) LEFT JOIN EqpTrnsVndr ON EquipTransfer.TransId = EqpTrnsVndr.TransId) INNER JOIN (Equipment INNER JOIN EquipTrans ON Equipment.EquipId = EquipTrans.SlctEqpId) ON EquipTransfer.TransId = EquipTrans.TransId
GROUP BY Equipment.EquipId, Equipment.EquipType, Equipment.SerialNo, EquipTrans.SlctEqpId
ORDER BY Last(EquipTransfer.ShipDate) DESC;
THIS IS AS FAR AS I’VE BEEN ABLE TO GO USING MAX.
SELECT EquipTrans.SlctEqpId, Max(EquipTransfer.ShipDate) AS MaxOfShipDate
FROM EquipTransfer LEFT JOIN EquipTrans ON EquipTransfer.TransId = EquipTrans.TransId
GROUP BY EquipTrans.SlctEqpId;
THIS IS THE QUERY I WAS USING:
SELECT Last(EquipTransfer.ShipDate) AS LastOfShipDate, Last(EquipTrans.TransId) AS LastOfTransId, Last(EquipTransfer.JobNo) AS LastOfJobNo, Equipment.EquipId, Equipment.EquipType, Equipment.SerialNo, Last(EquipTrans.ToMaster) AS LastOfToMaster, Last(EquipTrans.Comments) AS LastOfComments, EquipTrans.SlctEqpId, Last(EqpTrnsVndr.VendorId) AS LastOfVendorId, Last(EquipTrans.RMA) AS LastOfRMA, Last(EqpTrnsVndr.Mas)
AS LastOfMas, Last(qlkpJobNo.Manager) AS LastOfManager, Last(qlkpJobNo.MajorLoc) AS LastOfMajorLoc
FROM ((EquipTransfer INNER JOIN qlkpJobNo ON EquipTransfer.JobNo = qlkpJobNo.JobNo) LEFT JOIN EqpTrnsVndr ON EquipTransfer.TransId = EqpTrnsVndr.TransId) INNER JOIN (Equipment INNER JOIN EquipTrans ON Equipment.EquipId = EquipTrans.SlctEqpId) ON EquipTransfer.TransId = EquipTrans.TransId
GROUP BY Equipment.EquipId, Equipment.EquipType, Equipment.SerialNo, EquipTrans.SlctEqpId
ORDER BY Last(EquipTransfer.ShipDate) DESC;
THIS IS AS FAR AS I’VE BEEN ABLE TO GO USING MAX.
SELECT EquipTrans.SlctEqpId, Max(EquipTransfer.ShipDate) AS MaxOfShipDate
FROM EquipTransfer LEFT JOIN EquipTrans ON EquipTransfer.TransId = EquipTrans.TransId
GROUP BY EquipTrans.SlctEqpId;