How to add another field to a max query result without grouping

B

Barry A&P

I am trying to create a query to find the most recent locationID for each
item with a TrkID

but i can only seem to get it with two queries then join it back on lochist
date and TRKiD Is this the best way??
Here is the query to get the max date for each TRKID isnt there a clean way
to add in one more field "LocationID" ??

SELECT TRKID, Max(T_LocationHistory.LocHistDate) AS MaxOfLocHistDate
FROM T_LocationHistory
GROUP BY TRKID;

if i do i get the max date for each unique TrkID and LocationID combo

Thanks
 
B

Barry A&P

Why does it seem you cant find the answer till you post a question??
Looked harder and got it with this

SELECT T_LocationHistory.TRKID, T_LocationHistory.LocationID,
T_LocationHistory.LocHistDate
FROM T_LocationHistory INNER JOIN
[SELECT T_LocationHistory.TRKID, Max(T_LocationHistory.LocHistDate) AS
MaxOfLocHistDate
FROM T_LocationHistory
GROUP BY T_LocationHistory.TRKID]. as Q
ON T_LocationHistory.Lochistdate = Q.MaxOfLocHistDate and
T_LocationHistory.trkid = Q.trkid;

Thank You John Spencer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top