Solved

Add contents of one table's column to another table

Posted on 2007-03-26
4
264 Views
Last Modified: 2010-03-20
Sorry, I'm a real newbie to SQL. I have two tables with the same columns and would like to do the following:

Add the contents of Table2.ColumnX to Table1.ColumnX where Table1.pkID = Table2.pkID (all the rows are the same in both tables except for the values of ColumnX)

Thanks in advance for any help!
0
Comment
Question by:siono3
  • 3
4 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 18796591
Try this:

SELECT Table2.ColumnX + Table1.ColumnX  as YourSum
FROM  Table1 Inner Join Table2 ON Table1.pkID = Table2.pkID
0
 
LVL 61

Expert Comment

by:mbizup
ID: 18796599
If you actually need to update Table1's data, not just a select query, make a backup and try this:

Update Table1 Inner Join Table2 ON Table1.pkID = Table2.pkID
SET  Table2.ColumnX  = Table2.ColumnX + Table1.ColumnX  
0
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 18796611
Correction... The update query I posted before will update table2's data.  This will update table1:

Update Table1 Inner Join Table2 ON Table1.pkID = Table2.pkID
SET  Table1.ColumnX  = Table2.ColumnX + Table1.ColumnX  
0
 

Author Comment

by:siono3
ID: 18799320
Thanks mbizup,

That almost worked. For some reason I had to tweak it a little bit and ended up with the following:
UPDATE    Table1
SET              Table1.ColumnX = Table2.ColumnX
FROM         Table1 INNER JOIN
                      Table2 ON Table1.pkID = Table2.pkID

but you set me on the right track...
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question