SQL Query: How to check for the Join condition in ON clause only if a condition is true

Viewed 59

I want to join tables and get the result but I have a condition like, in the ON clause one join condition should be checked only if another condition is true. For Eg:

SELECT a.plan_id, a.plan_name, a.sale_s1, b.sale_s1
from (SELECT plan_id, plan_name, sale_s1, sale_s2
     from RESULT_TABLE
      where plan_id = 1245 and sale_id = (any integer value))a
JOIN (SELECT plan_id, plan_name, sale_s1, sale_s2
        from RESULT_TABLE where plan_id = 1245 and sale_id=(any integer value))b
    ON a.plan_id = b.plan_id AND (2nd CONDITION: SHOULD BE CHECKED only if(sale_id=0))

All the columns are NOT NULLABLE

Please help me with this, how to achieve the case for that 2nd condition.

1 Answers

If sale_id is NOT NULL:

AND (sale_id <> 0 OR (2nd CONDITION))
Related