Zkuste
SELECT i.id,
i.address,
i.status,
p.max_date contract_date,
p.basis_value contract_price,
e.max_date estimate_date,
e.basis_value estimate_value
FROM Instructions i LEFT JOIN
(
SELECT q1.instruction_id, max_date, basis_value
FROM Estimates e JOIN
(
SELECT instruction_id, MAX(basis_date) max_date
FROM Estimates
WHERE basis = 'CustomerEstimate'
GROUP BY instruction_id
) q1 ON e.instruction_id = q1.instruction_id AND e.basis_date = q1.max_date
) e ON i.id = e.instruction_id LEFT JOIN
(
SELECT q2.instruction_id, max_date, basis_value
FROM Estimates e JOIN
(
SELECT instruction_id, MAX(basis_date) max_date
FROM Estimates
WHERE basis = 'ContractPrice'
GROUP BY instruction_id
) q2 ON e.instruction_id = q2.instruction_id AND e.basis_date = q2.max_date
) p ON i.id = p.instruction_id
Výstup:
| ID | ADDRESS | STATUS | CONTRACT_PRICE | CONTRACT_DATE | ESTIMATE_VALUE | ESTIMATE_DATE | ---------------------------------------------------------------------------------------------------------------------------- | 1 | 27 TAYLOR ROAD, ALBION PARK NSW 2527 | InProgress | 140000 | 2013-01-03 | 145000 | 2013-02-09 |
Zde je SQLFiddle demo.