Question : SQL query - Get last record based on date

Hi I have a MS ACCESS tabel "Inspections"

When a property is visisted an inspection form is completed along with date and by who.

The query I am struggling with is to pull out the last inspection (based on date) for each property along with who it was done by.

I thought that the following was working but it isn't. It correctly only shows the last inspection, but it associated the wrong property manager with it.

PLEASE HELP!!!  

SELECT Inspections.Ref, Max(Inspections.[Inspection Date]) AS [MaxOfInspection Date], Max(tblStaff.name) AS MaxOfname
FROM tblStaff RIGHT JOIN Inspections ON tblStaff.staff_id = Inspections.Propertymanager
GROUP BY Inspections.Ref;

Answer : SQL query - Get last record based on date

OK I had to revert to the design of the query to see what I was missing out.
Cheers, Andrew
1:
2:
3:
SELECT I.Ref, I1.I_Date AS [Inspection Date], S.Name
FROM (Inspections AS I INNER JOIN (SELECT Ref, MAX([Inspection Date]) AS I_Date FROM Inspections GROUP BY Ref) AS I1 ON (I.[Inspection Date] = I1.I_Date) AND (I.Ref = I1.Ref)) 
INNER JOIN tblStaff AS S ON I.PropertyManager = S.Staff_ID
Open in New Window Select All
Random Solutions  
 
programming4us programming4us