左边的“IF”条件加入“SAS”proc sql

时间:2021-01-24 11:40:07

I have a table a, which contains columns a1 and a2.

我有一个表a,其中包含列a1和a2。

I have a table b, which contains columns b1 and b2.

我有一个表b,其中包含b1和b2列。

I want to left join b to a on condition1 if a2 is null and on condition2 if a2 is not null.

如果a2为null,我想将join b连接到on条件1;如果a2不为null,我想将条件2连接到条件2。

How can i construct this query?

我该如何构建此查询?

1 个解决方案

#1


1  

If I understand correctly:

如果我理解正确:

proc sql;
    select . . .
    from a left join
         b
         on (a2 is null and condition1) or
            (a2 is not null and condition2);

This is a direct translation of your requirements. In general, the following often has better performance because this can make better use of indexes (depending on the nature of the conditions):

这是您的要求的直接翻译。通常,以下通常具有更好的性能,因为这可以更好地使用索引(取决于条件的性质):

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);

#1


1  

If I understand correctly:

如果我理解正确:

proc sql;
    select . . .
    from a left join
         b
         on (a2 is null and condition1) or
            (a2 is not null and condition2);

This is a direct translation of your requirements. In general, the following often has better performance because this can make better use of indexes (depending on the nature of the conditions):

这是您的要求的直接翻译。通常,以下通常具有更好的性能,因为这可以更好地使用索引(取决于条件的性质):

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);