如何合并
下表是我把两个表full join出来的结果,怎么让它变成表2中的结果?
表1:
A B C D
0001 45 0001 37
0026 49 0026 326
0031 12 NULL NULL
0032 57 0032 63
0037 12 0037 3
NULL NULL 0042 35
NULL NULL 0043 11
表2:
0001 45 37
0026 49 326
0031 12 NULL
0032 57 63
0037 12 3
0042 NULL 35
0043 NULL 11
------解决方案--------------------select coalesce(a,c) A,
B,
D
from [表1] a full join [表2] b
on a.id=b.id
------解决方案--------------------select (case when A is null then C else A end),B,D from Ta
------解决方案--------------------create table gjlsss
(
A varchar(10),
B varchar(10),
C varchar(10),
D varchar(10)
)
insert into gjlsss
select '0001 ', '45 ' , '0001 ', '37 ' union all select
'0026 ' , '49 ' , '0026 ', '326 ' union all select
'0031 ' , '12 ' , 'NULL ', 'NULL ' union all select
'0032 ' , '57 ' , '0032 ', '63 ' union all select
'0037 ', '12 ' , '0037 ', '3 ' union all select
'NULL ' , 'NULL ' , '0042 ' , '35 ' union all select
'NULL ', 'NULL ', '0043 ' , '11 '
select (case A when 'null ' then C else A end)as AC ,B,D from gjlsss
===============================
0001 45 37
0026 49 326
0031 12 NULL
0032 57 63
0037 12 3
0042 NULL 35
0043 NULL 11
------解决方案----------------------假設列名都為ID, Value
Select
IsNull(A.ID, B.ID) As ID,
A.Value,
B.Value
From
表1 A
Full Join
表2 B
On A.ID = B.ID