DISTINCT ON
of is je vriend. Hier is een oplossing met correct syntaxis:
SELECT a.id, b.updated, b.col1, b.col2
FROM table_a as a
LEFT JOIN (
SELECT DISTINCT ON (table_a_id)
table_a_id, updated, col1, col2
FROM table_b
ORDER BY table_a_id, updated DESC
) b ON a.id = b.table_a_id;
Of, om de hele rij te krijgen van table_b
:
SELECT a.id, b.*
FROM table_a as a
LEFT JOIN (
SELECT DISTINCT ON (table_a_id)
*
FROM table_b
ORDER BY table_a_id, updated DESC
) b ON a.id = b.table_a_id;
Gedetailleerde uitleg voor deze techniek en alternatieve oplossingen onder deze nauw verwante vraag:
Selecteer de eerste rij in elke GROUP BY-groep?