?
Solved

Add contents of one table's column to another table

Posted on 2007-03-26
4
Medium Priority
?
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 750 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

800 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