Join based on case expression
08:46 26 Mar 2019

I want to have 2 join condition between two tables, first join condition should work always but the second Join condition should work only if condition is satisfied other wise give the result based on first two join.

I have two way one is directly give two condition and then add case statement for third join

Second I tried to have case expression after ON. For first case I had 1 join condition and for second case 2 put 2 join conditions.

SELECT *,IND.bill_to_id  FROM  txn_sales_shippedorders_intl shippedorders
LEFT JOIN ( SELECT cust_sid, max(cust_key) cust_key
                  ,cust_nbr
              FROM com_hub.ref_customer_hco_intl
             GROUP BY cust_sid, cust_nbr
          ) cust ON cust.cust_sid = regexp_replace(shippedorders.cust_align_sid, "\\.0*", '')
                                --AND cust.country_cd = shippedorders.country_cd
                                AND cust.src_sys_id=shippedorders.src_sys_id
                                                                                                                                
LEFT JOIN 
         (SELECT CUSTOMER,ship_to_id, bill_to_id
            FROM com_lake.ref_india_customer_crossmap
         ) ind ON case when substr(SDVR02,1,2) ='IM' 
                           and ind.ship_to_id = cust.cust_nbr then 1
                       WHEN substr(SDVR02,1,2) ='ER' and ind.CUSTOMER = cust.cust_name then 1
                    else 0
                  end 1
sql hive