Necromancy because answers are out of date.
Use the LATERAL keyword introduced in PG 9.3
left | right | internal JOIN LATERAL
I will explain with an example:
Assuming you have a Contacts table.
Contacts now have organizational units.
They can have one OU at a certain point in time, but N OU at N time points.
Now, if you need to request contacts and OU for a period of time (not a reporting date, but a date range), you can increase the number of entries by N times if you just made a left connection.
So, to display the OU, you just need to attach the first OU for each contact (where what should be the first is an arbitrary criterion - for example, when accepting the last value, this is just another way to say the first value when sorting in descending date).
In a SQL server, you would use cross-application (or rather OUTER APPLY, since we need a left join), which will call a table function on each row to be joined.
SELECT * FROM T_Contacts --LEFT JOIN T_MAP_Contacts_Ref_OrganisationalUnit ON MAP_CTCOU_CT_UID = T_Contacts.CT_UID AND MAP_CTCOU_SoftDeleteStatus = 1 --WHERE T_MAP_Contacts_Ref_OrganisationalUnit.MAP_CTCOU_UID IS NULL -- 989 -- CROSS APPLY -- = INNER JOIN OUTER APPLY -- = LEFT JOIN ( SELECT TOP 1 --MAP_CTCOU_UID MAP_CTCOU_CT_UID ,MAP_CTCOU_COU_UID ,MAP_CTCOU_DateFrom ,MAP_CTCOU_DateTo FROM T_MAP_Contacts_Ref_OrganisationalUnit WHERE MAP_CTCOU_SoftDeleteStatus = 1 AND MAP_CTCOU_CT_UID = T_Contacts.CT_UID ORDER BY MAP_CTCOU_DateFrom ) AS FirstOE
In PostgreSQL starting with version 9.3, you can do this too - just use the LATERAL keyword to achieve the same:
SELECT * FROM T_Contacts --LEFT JOIN T_MAP_Contacts_Ref_OrganisationalUnit ON MAP_CTCOU_CT_UID = T_Contacts.CT_UID AND MAP_CTCOU_SoftDeleteStatus = 1 --WHERE T_MAP_Contacts_Ref_OrganisationalUnit.MAP_CTCOU_UID IS NULL -- 989 LEFT JOIN LATERAL ( SELECT --MAP_CTCOU_UID MAP_CTCOU_CT_UID ,MAP_CTCOU_COU_UID ,MAP_CTCOU_DateFrom ,MAP_CTCOU_DateTo FROM T_MAP_Contacts_Ref_OrganisationalUnit WHERE MAP_CTCOU_SoftDeleteStatus = 1 AND MAP_CTCOU_CT_UID = T_Contacts.CT_UID ORDER BY MAP_CTCOU_DateFrom LIMIT 1 ) AS FirstOE