Hi Grigore
The most reliable way I've found is to:
SELECT ;
Id, ;
Serial, ;
Model, ;
LatestRegistration.RegNumber, ;
LatestRegistration.RegDate ;
FROM Cars
LEFT OUTER JOIN ;
(SELECT ;
Regtmp.CarID, ;
Regtmp.RegNumber, ;
Regtmp.RegDate ;
FROM Registrations Regtmp ;
INNER JOIN ;
(SELECT ;
CarID, ;
MAX(RegDate) AS MaxDate ;
FROM ;
Registrations ;
GROUP BY ;
CarID) MaxReg ;
ON Regtmp.CarId = MaxReg.CarId ;
AND Regtmp.RegDate = MaxReg.MaxDate) ;
LatestRegistration ;
ON Cars.Id = LatestRegistration.CarId ;
This approach guarantees you have the fields from the registration
record for the latest registration. The normal group by approach will
give you the max registration date, but not necessarily the
corresponding registration number.
HTH
Mike Yearwood
Fox Ridge Software
> I have two tables, 1-to-many related. Parent table contains car information,
> child table contains the registration numbers, with their date, as they
> change.
>
> Parent table fields:
>
> Id, Serial, Model
>
> Child table fields:
>
> Id, CarId, RegNumber, RegDate
>
>
> How the heck the SQL that returns a list of the cars with their most recent
> registration number looks like? I just can't figure it.
>
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/profox
OT-free version of this list: http://leafe.com/mailman/listinfo/profoxtech
Searchable Archive: http://leafe.com/archives/search/profox
This message: http://leafe.com/archives/byMID/profox/[EMAIL PROTECTED]
** All postings, unless explicitly stated otherwise, are the opinions of the
author, and do not constitute legal or medical advice. This statement is added
to the messages for those lawyers who are too stupid to see the obvious.