2

I have two tables: contracts and contract_descriptions.

On contract_descriptions there is a column named contract_id which is equal on contracts table records.

I am trying to join the latest record on contract_descriptions:

SELECT *
FROM contracts c
LEFT JOIN contract_descriptions d ON d.contract_id = c.contract_id
AND d.date_description =
  (SELECT MAX(date_description)
   FROM contract_descriptions t
   WHERE t.contract_id = c.contract_id)

It works, but is it the performant way to do it? Is there a way to avoid the second SELECT?

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59

2 Answers2

2

You could also alternatively use DISTINCT ON:

SELECT * FROM contracts c LEFT JOIN (
    SELECT DISTINCT ON (cd.contract_id) cd.* FROM contract_descriptions cd
    ORDER BY cd.contract_id, cd.date_description DESC
) d ON d.contract_id = c.contract_id

DISTINCT ON selects only one row per contract_id while the sort clause cd.date_description DESC ensures that it is always the last description.

Performance depends on many values (for example, table size). In any case, you should compare both approaches with EXPLAIN.

clemens
  • 16,716
  • 11
  • 50
  • 65
2

Your query looks okay to me. One typical way to join only n rows by some order from the other table is a lateral join:

SELECT *
FROM contracts c
CROSS JOIN LATERAL
(
  SELECT *
  FROM contract_descriptions cd
  WHERE cd.contract_id = c.contract_id
  ORDER BY cd.date_description DESC
  FETCH FIRST 1 ROW ONLY
) cdlast;
Thorsten Kettner
  • 89,309
  • 7
  • 49
  • 73