繁体   English   中英

匹配四列作为完成左外部联接的条件

[英]Match four columns as a condition to complete left outer join

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

换句话说,如果四个列匹配,则updatenotice应该等于第二个表(表b )中的值。如果其中一个列不匹配,则不要updatenotice第二个表的值,而应保持第一个表中的updatenotice (表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;

案例声明必须包含在选择部分中:

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'作为默认值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM