I'am searching for the best way to filter a table by a MAX date. Is it better to find with a select in the where
SELECT *
FROM
table1 foo
WHERE
foo.date_tmp =
(
SELECT
MAX (tmp.date_tmp )
FROM
table2 tmp
WHERE
tmp.table_id = foo.id
)
Or make it with a with
WITH tmp_max_date AS
(
SELECT
tmp.table_id,
MAX(date_tmp) date_tmp
FROM
table2 tmp
GROUP BY
tmp.table_id
)
SELECT foo.*
FROM
table1 foo
INNER JOIN tmp_max_date tmd ON
tmd.table_id = foo.id
AND tmd.date_tmp = foo.date_tmp
Does anyone know the best way? IMO it's the second one but I'm not sure.