我试图将一些值从一个表插入到另一个表中,并想知道如何使其成为可能。
表A有4列,其中有A_1和A_2,在某些行中空格。
表B有3列,其中包含B_1和B_2,填充了所有这些列中的。
我希望将来自B_1和B_2的值分别插入到缺少行的A_1和A_2中。我确实有相同的id可在这两个可加入的目的。
我在想以下几点
proc sql;
insert into A
(A_1 , A_2)
as select B_1 , B_2
from B
where A_1 = '' and A_2 = ''
;
quit;发布于 2015-01-29 19:06:11
我不熟悉SAS,您也没有列出您的RBDMs,但是查询的基本思想是:
update tableA
set a_1 = b.b_1 ,
a_2 = b.b_2
from tableA a
inner join tableB b on a.Id = b.Id
where a.a_1 is null
and a.a_2 is null您有一个insert语句的开始,但是除非我误解了您的场景,否则如果两个表之间都存在if,听起来您实际上是在寻找更新。
请注意,它将"id“字段上的表a和b连接起来,然后用b.b_1更新b.b_1,用b.b_2更新a.a_2,只有在a.a_1和a.a_2都是null的情况下--我不确定您的意思是空字符串还是空字符串。如果您的意思是空字符串,请使用a.a_1 is null和a.a_1 = ''关闭
发布于 2015-01-29 21:15:42
data a;
key = 1;
a_1 = 4;
a_2 = 6;
output;
key = 2;
a_1 = .;
a_2 = 6;
output;
key = 3;
a_1 = 4;
a_2 = .;
output;
run;
data b;
key = 1;
b_1 = 14;
b_2 = 62;
output;
key = 2;
b_1 = 3;
b_2 = 64;
output;
key = 3;
b_1 = 54;
b_2 =6 ;
output;
run;
proc sql;
create table a as
select
coalesce(a_1,b_1) as a1,
coalesce(a_2,b_2) as a2
from a
left join b
on
(
a.key = b.key
);quit;我使用左联接,因为我不想从a中删除行,以防b中缺少行。
本程序将给您一个警告:
WARNING: This CREATE TABLE statement recursively
references the target table. A consequence of
this is a possible data integrity problem.但从我的经验来看,效果很好。
https://stackoverflow.com/questions/28222386
复制相似问题