Dynamically Update all Records from One table to another Table - SQL Server 2008

Hello Experts,

I am using SQL Server 2008 and have two tables, table_1 and table_2.


ID      Weight
A      20.0
B      40.0      
C      30.2


ID      Weight
A       1.1
B      -1.5
C      6.2
B      2.5
A      -6.3            

I would like to update the Weight column in table_1 (add to current value) from the weight column in table_2 based on the ID columns in table_1 and table_2.

I would like to do this by Grouping table_2 by ID then updating the Weight column of each corresponding ID in table_1.  

Table_1 should look like this when applied:
ID      Weight
A      14.8
B      41
C      36.4

Is there a way to do this implicitly? In other words, Weights of ALL ID values in table_2 get grouped then applied (added to) Weight columns of the corresponding table_1 ID values?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
WITH cteTable2 AS
	SELECT ID, SUM([Weight]) AS [Weight]
	FROM Table_2
UPDATE Table_1
SET [Weight] = [Weight] + T2.[Weight]
FROM Table_1 AS T1
INNER JOIN cteTable2 AS T2
	ON T1.ID = T2.ID

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SaxitalisAuthor Commented:
That was it sir - Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.