2017-02-12 31 views
0

我有一個表a,它包含列a1a2「IF」條件左加入「SAS」進程sql

我有一個表b,其中包含列b1b2

我想left joinbacondition1如果a2 is nullcondition2如果a2 is not null

我該如何構造這個查詢?

+0

樣本數據和期望的結果將有助於闡明您想要執行的操作。 –

回答

1

如果我理解正確:

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