我有一个表a
,其中包含列a1
和a2
。
我有一个表b
,其中包含列b1
和b2
。
我想left join
b
a
condition1
如果a2 is null
,condition2
如果a2 is not null
.
如何构造此查询?
如果我
理解正确:
proc sql;
select . . .
from a left join
b
on (a2 is null and condition1) or
(a2 is not null and condition2);
这是对您的要求的直接翻译。 通常,以下内容通常具有更好的性能,因为这可以更好地利用索引(取决于条件的性质(:
proc sql;
select a.*, coalesce(b1.b2, b2.b2) as b2
from a left join
b b1
on (a2 is null and condition1) left join
b b2
on (a2 is not null and condition2);