Solved

Add contents of one table's column to another table

Posted on 2007-03-26
4
276 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

695 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