2011-10-20 123 views
0

我有兩個具有相同列名的表我必須爲兩個表上的某些特定條件添加oprId列值。將多個表中的多個列添加到一列中

表1

something oprId 

abc   1 
qwe   2 

表2

something oprId 

abc   2 
qwe   5 

結果應該是

oprId 
3 
7 
+0

我想你已經遺漏了問題的重要部分。 3和7從哪裏來? –

+0

它應該是oprId 1 + 2和2 + 5的總和。 – Naveen

+0

好的。所以你加入'xyz'到'abc'和'ghj'到'qwe'?這些值參數是查詢嗎? –

回答

1
declare @T1 table (something varchar(3), oprId int) 
declare @T2 table (something varchar(3), oprId int) 

insert into @T1 values ('abc', 1),('qwe', 2) 
insert into @T2 values ('abc', 2),('qwe', 5) 

select T1.oprId+T2.oprId as oprId 
from @T1 as T1 
    inner join @T2 as T2 
    on T1.something = T2.something 

結果:

oprId 
------ 
3 
7 
0
SELECT ISNULL(A.something,B.something) Something, 
     ISNULL(A.oprId,0)ÍSNULL(B.oprId,0) oprId 
FROM Table1 A 
FULL JOIN Table2 B 
ON A.something = B.something 
相關問題