there's a problem with your solution -- the value of B.appraised that is returned is ~not~ necessarily the most recent appraisal
this is the "hidden column" problem outlined here: http://dev.mysql.com/doc/refman/5.0/...n-columns.html
the correct way to select the latest B row would be as follows...
Code:
SELECT A.id
, A.title
, B.appraised
, SUM(C.amount) AS paid
FROM A
INNER
JOIN C
ON C.aid = A.id
LEFT OUTER
JOIN ( SELECT aid
, MAX(appraisalDate) AS latest
FROM B
GROUP
BY aid ) AS Bm
ON Bm.aid = A.id
LEFT OUTER
JOIN B
ON B.aid = Bm.aid
AND B.appraisalDate = Bm.latest
GROUP
BY A.id
joining to a MAX subquery as a derived table ensures that only one row from B is joined to each A, and thus the GROUP BY on A.id is sufficient
Bookmarks