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
 General SQL Server Forums
 New to SQL Server Programming
 Summing matching data from two tables

Author  Topic 

FishyFish
Starting Member

2 Posts

Posted - 2011-04-13 : 06:02:53
Hi,

I've reached the limit of my already limited SQL coding skills, and wonder if someone could help me with the following please?

I have two tables of data, both in the same format, e.g.

Table1:

UniqueID FirstName LastName Value
1111 Steve Smith 150
1112 Anthony Craven 355
1113 Mary Greene 500
1114 Jame Grayson 55


Table2:

UniqueID FirstName LastName Value
1111 Steve Smith 200
1113 Mary Greene 180
1116 Stuart Pearce 450
1117 Richard Tong 605


What I want to do is get a list of results summing the values from both tables where theres a matching ID present, e.g.


UniqueID FirstName LastName TotalValue
1111 Steve Smith 350
1113 Mary Greene 680


So the query has found matching UniqueID values in both tables and then summed the Value column for both.

Any help on how I can code this is much appreciated.

Thanks!

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-04-13 : 06:44:00
Try this

SELECT
t1.UniqueID
,t1.FirstName
,t1.LastName
,t1.Value+t2.Value as TotalValue
from table1 t1
INNER JOIN table2 t2 on t1.UniqueID=t2.UniqueID

--Ranjit
Go to Top of Page

FishyFish
Starting Member

2 Posts

Posted - 2011-04-13 : 08:07:15
Thanks Ranjit, works perfectly.
Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-04-13 : 10:20:45
welcome FishyFish

--Ranjit
Go to Top of Page
   

- Advertisement -