I'm attempting to join two tables where all the records in table one have unique ids and table two can contain multiple records for an id in table one (ids in table one may not have any corresponding records in table two).
I would like to join only the most recent record in table two.
SELECT id, MAX(date) AS DATE FROM table_two
Above returns the most recent records for a particular id.
SELECT * FROM table_one t1 LEFT JOIN table_two t2 ON t1.id = t2.id
Above returns the joined table but returns the duplicates as well.
How can I construct the join statement to return only the most recent records in table two?
Something like this:
SELECT * FROM table_one t1 LEFT JOIN table_two t2 ON t1.id = t2.id, MAX(t2.date) AS date GROUP BY date
Above returns an error: ERROR: aggregate functions are not allowed in functions in FROM
I know I can use a subquery to get the most recent record but not sure of the most efficient way to do this.