postgresql - Postgres: left join with order by and limit 1 -
I have the position:
The list of companies in Table 1 is in the table 2 addresses A list of table 3 table 1 and table 2 has an N connection, with field 'start' and 'end'.
Because companies can go ahead over time, having a left for each of them has many records for each company.
start
and end
fields are never copied to find the latest address solution using DESC
using ORDER , And there is a LIMIT 1
to delete the old address.
This works fine The query can only be brought to 1 company but I need a query that brings all the table 1 records, associated with their current table2 address. Therefore, to remove old data (AFAK) should be omitted.
Any ideas how can I create a clause to not create doubling Table 1 companies and bring the latest address?
Use a dependent subqueries with the maximum () function in the joining condition. Something like this in this example:
C.company_id = r.company_id and r on the relationship relationship except the select companies. "Start" = (Select Max ("start") relationship with r1 WHERE c.company_id = r1.company_id) INNER JOIN on an .address_id = r.address_id
demo:
Comments
Post a Comment