postgresqlsql-order-byleft-joinforeign-key-relationshipduplicates

Postgres: left join with order by and limit 1


I have the situation:

Table1 has a list of companies.
Table2 has a list of addresses.
Table3 is a N relationship of Table1 and Table2, with fields 'begin' and 'end'.

Because companies may move over time, a LEFT JOIN among them results in multiple records for each company.

begin and end fields are never NULL. The solution to find the latest address is use a ORDER BY being DESC, and to remove older addresses is a LIMIT 1.

That works fine if the query can bring only 1 company. But I need a query that brings all Table1 records, joined with their current Table2 addresses. Therefore, the removal of outdated data must be done (AFAIK) in LEFT JOIN's ON clause.

Any idea how I can build the clause to not create duplicated Table1 companies and bring latest address?


Solution

  • I managed to solve it using Windows Function:

    WITH ranked_relationship AS(
        SELECT
            *
            ,row_number() OVER (PARTITION BY fk_company ORDER BY dt_start DESC) as dt_last_addr
        FROM relationship
    )
    
    SELECT
        company.*
        address.*,
        dt_last_addr as dt_relationship
    FROM
        company
        LEFT JOIN ranked_relationship as relationship
                ON relationship.fk_company = company.pk_company AND dt_last_addr = 1
        LEFT JOIN address ON address.pk_address = relationship.fk_address
    

    row_number() creates an int counter for each record, inside each window based to fk_company. For each window, the record with latest date comes first with rank 1, then dt_last_addr = 1 makes sure the JOIN happens only once for each fk_company, with the record with latest address.

    Window Functions are very powerful and few ppl use them, they avoid many complex joins and subqueries!