Skip to content
Advertisement

Selecting columns from tables based on max value of another column

I have two tables and I want column input from first_table and column output from second_table based on the latest finished time

first_table:

second_table

result_table

I am trying to use this query:

Advertisement

Answer

You can use a correlated subquery in the on clause of the join:

For performance here, you want an index on second_table(id, finished_time).

This can also be expressed with a not exists condition:

Finally, another option is to use a window function for ranking

You can play around with the solutions and pick the one that you understand better, or that performs faster.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement