简体   繁体   中英

How to Check join value for null and lookup in the other tables

If the join values of accomm_bk and type_bk is Null then how to lookup values in tables say lookup_accomm_bk , lookup_type_bk .

Any help will be appreciated.

select accomm_bk,type_bk
from 
staging.contract a 
left join dim.accomm_dim b on (a.accomm_id)= b.accomm_hash
left join dim.type_dim c on (a.accomm_id)= c.type_hash

IF Result is NULL, then how to lookup staging.contract a with tables lookup_accomm_bk for column accomm_bk and lookup_type_bk for column type_bk and get values.

Example

accomm_bk | type_bk
--------------------
NULL      | NULL

If Result is NULL, then how to lookup staging.contract a with tables lookup_accomm_bk for column accomm_bk and lookup_type_bk for column type_bk and get values.

You would need to add two more LEFT JOIN s to your query to link the contract table to tables lookup_accomm_bk and lookup_type_bk .

Then use the COALESCE function to display the looked up values if they can't be found in accomm_dim and type_dim .

Here is a skeleton for the query (you need to define the proper ON clauses for the additional LEFT JOIN s) :

select 
    COALESCE(b.accomm_bk, lb.accomm_bk),
    COALESCE(c.type_bk, lc.type_bk)
from 
    staging.contract a 
    left join dim.accomm_dim b on (a.accomm_id)= b.accomm_hash
    left join dim.type_dim c on (a.accomm_id)= c.type_hash
    left join dim.lookup_accomm_bk lb on ...
    left join dim.lookup_type_bk lc on ... 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM