SQL 根据 if else if 条件连接表

SQL join tables based on if else if conditions

刚开始做 SQL 事情所以请原谅。

我想创建一个 SQL 查询,该查询根据以下匹配逻辑将 table A 从 table B 的列连接到 table A:

B.Source = ‘SOURCE1’ and A.NameCode= B.Code

如果上面的 return NULL 那么我想匹配:

B.Source <> ‘SOURCE1’ and A.UEN = B.UEN**

关于如何构建这个的任何帮助? 我目前有一个 union all select 查询,可以根据上述条件获取值。我应该在加入过程中使用 If/or/case_when 吗?

我认为可能有帮助并且我已经看过的几个问题是:

How to perform a LEFT JOIN in SQL Server between two SELECT statements?

Using IS NULL or IS NOT NULL on join conditions - Theory question

但我什么也想不出来:( 太感谢了!

尝试这样的事情:

SELECT * 
FROM A
JOIN B ON (
    (B.Source = 'Source1' AND A.NameCode = B.Code) OR
    (B.Source <> 'Source1' AND A.UEN = B.[UEN**]) --Not sure what the ** is?  Part of the field name?
)