Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 how to add two table in one

Author  Topic 

zeyi
Starting Member

5 Posts

Posted - 2009-01-12 : 13:11:21
Hi, there.

I have two tables,

table1
PGname sell_sum
AA 10
BB 15
CC 17
DD 11

table2
PGname sell_sum
AA 10
CC 11
FF 5
GG 12

I want to have table3 or update table1 or table2, the expected table is keep PGname and add sell_sum if the PGname exist in both, and keep PGname and sell_sum if only in one table, like this:

Table
PGname sell_sum
AA 20
BB 15
CC 28
DD 11
FF 5
GG 12

please offer the query how to do it? million thanks!

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2009-01-12 : 13:19:25
Select PGname,Sum(sell_sum)as Sumsell
from
(Select Pgname,sell_sum from table1
union all
Select Pgname,sell_sum from table2)t
Group by PGname
Order by PGname
Go to Top of Page
   

- Advertisement -