Allen_Delon
Allen_Delon

Reputation: 37

How to sum a specific row from one table to another in SQL Server

I need to add row 1 from Table2 to row 4 in Table1.

Result for row 4 in Table1 will be: DD1  9   5   7
Table1                                  Table2
=======                                 =======
Category C1     C2      C3              Category C1    C2    C3
A        8      4       5               DD1      1     1     2
B        1      0       3
C        2      1       0
DD1      8      4       5

Finally table1 becomes...

Table1
=======
Category C1     C2      C3
A        8      4       5
B        1      0       3
C        2      1       0
DD1      9      5       7

Any help would be much appreciated.

Upvotes: 1

Views: 77

Answers (1)

ToTa
ToTa

Reputation: 3334

Try this:

UPDATE T1 
SET 
  T1.C1 = T1.C1 + T2.C1,
  T1.C2 = T1.C2 + T2.C2,
  T1.C3 = T1.C3 + T2.C3
FROM Table1 AS T1
INNER JOIN Table2 AS T2 
ON T1.Category = T2.Category 
WHERE T1.Category = 'DD1'

Upvotes: 2

Related Questions