匹配四列作为条件完成左外连接

Match four columns as a condition to complete left outer join

我需要将两个 table 与第一个 table 中的所有列(左外部联接)组合在一起,如果四列中的值匹配,则只需要将第二个中的一列组合起来。

换句话说,如果四列匹配,那么 updatenotice 应该等于第二列中的值 table (table b) 如果其中一列是不匹配则不加入第二个 table 的值,但保持 updatenotice 与第一个 table 相同(table a)。

我在 case 语句中遇到语法错误。

这是我的代码:

proc sql;
create table UseLimit_updates as
select *
from work.updated_mwh as a
left outer join work.archive_dups as b
   on a.updatenotice=b.updatenotice
   case when a.res_id=b.res_id
     and a.limit_start_date=b.limit_start_date
     and a.limit_end_date=b.limit_end_date
     and a.created_date=b.created_date
     then a.updatenotice=b.updatenotice
 else a.updatenotice='A'
end;
quit;

case语句必须包含在select部分:

select 
  case when b.updatenotice is null then a.updatenotice else b.updatenotice end,
  <rest of the columns of work.updated_mwh>
from work.updated_mwh as a
left join work.archive_dups as b
on 
  a.res_id=b.res_id and 
  a.limit_start_date=b.limit_start_date and 
  a.limit_end_date=b.limit_end_date and 
  a.created_date=b.created_date
end;

我觉得coalesce()更简洁:

proc sql;
create table UseLimit_updates as
    select . . .,
           coalesce(b.updatenotice, a.updatenotice, 'A') as updatenotice
    from work.updated_mwh a left join
         work.archive_dups b
         on a.updatenotice = b.updatenotice and
            a.limit_start_date = b.limit_start_date and
            a.limit_end_date = b.limit_end_date and
            a.created_date = b.created_date;
    end;

quit;

如果其他两个值都缺失,您的代码还建议您将 'A' 作为默认值。