[英]Insert into Specific Column from other table
我試圖將某些值從一個表插入到另一個表,並想知道如何使其成為可能。
表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;
我對SAS不熟悉,您沒有列出RBDM,但是查詢的基本思想是:
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
您具有插入語句的開頭,但是除非我對您的情況有誤解,否則聽起來好像您實際上是在尋找更新(如果兩個表之間都存在ID)。
請注意,只有在a.a_1和a.a_2均為null
情況下,此操作才會在“ id”字段上連接表a和b,然后用b.b_1更新a.a_1,並用b.b_2更新a.a_2。不確定是要輸入null還是空字符串。 如果您的意思是空字符串,請使用a.a_1 = ''
切換a.a_1 is null
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.
但是根據我的經驗,它可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.