简体   繁体   English

如何在其他表中检查联接值是否为空和查找

[英]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 . 如果accomm_bktype_bk的连接值为Null,则如何在表中查找值表示lookup_accomm_bklookup_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. 如果结果为null,那么如何查找staging.contract a有桌子lookup_accomm_bkaccomm_bklookup_type_bktype_bk和获取值。

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. 如果Result为NULL,则如何查找staging。使用表lookup_accomm_bk的accomm_bk列和表lookup_type_bk的type_bk列来契约a并获取值。

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 . 您将需要在查询中再添加两个LEFT JOIN ,以将contract表链接到表lookup_accomm_bklookup_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 . 如果在accomm_dimtype_dim找不到查找的值,则使用COALESCE函数显示查找的值。

Here is a skeleton for the query (you need to define the proper ON clauses for the additional LEFT JOIN s) : 这是查询的框架(您需要为其他LEFT JOIN定义适当的ON子句):

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 ... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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