Shalini
Shalini

Reputation: 33

Using self join in Postgres

How can I create the below JOIN in Postgres , This code is from Oracle and it has one join condition which I am not familiar with.

T3.u_ffm_sub_id(+) = T3.u_ffm_id(+)

It is like self join but (+) is on both the sides of condition, So I am confused how to create same in postgres.

Below is the complete code:

SELECT DISTINCT
   CASE
       WHEN MD.u_ffm_id = MD.u_ffm_sub_id THEN MD.patient
       ELSE T3.patient
   END
INTO lRet
FROM UPI.memdate MD,
   UPI.memdate T3
WHERE T3.hmo           = pHmo
AND T3.u_ffm_sub_id(+) = T3.u_ffm_id(+)
AND MD.hmo             = T3.HMO(+)
AND MD.u_ffm_sub_id    = T3.u_ffm_sub_id(+)
AND md.patient         = pPatient;

Upvotes: 0

Views: 173

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246133

These are outer joins, and they are easy to translate to standard conforming SQL.

  1. If the (+) is on the right side:

    FROM a, b
    WHERE a.x = b.y(+)
    

    gets translated to

    FROM a LEFT JOIN b
       ON a.x = b.y
    
  2. If the (+) is on the other side, it would be a RIGHT JOIN.

  3. If the (+) is on both sides:

    FROM a, b
    WHERE a.x(+) = b.y(+)
    

    gets translated to

    FROM a FULL JOIN b
       OM a.x = b.y
    

Upvotes: 2

Related Questions