views:

48

answers:

1

I want to get records from one table based on a record from another table. They both have SSN fields and I want to link them by SSN. Here is a rough example what I want to get:

SELECT SUM(Table1.Payments) 
  FROM Table1 
 WHERE Table1.SSN = Table2.SSN 
   AND Table2.City = 'New York'

I want to get the sum of the payments by a variable in this case, city.

+5  A: 
SELECT SUM(t1.Payments) 
from Table1 t1 
JOIN Table2 t2 on t1.SSN = t2.SSN and t2.City = 'New York'

You use an inner join, to get only the matching records where Table1 Payments are in Table2 with the same SSN.

If there are multiple records in Table2 for the SSN, then you may want to use an EXISTS query.

Select SUM(t1.Payments)
from Table1 t1 
where EXISTS( SELECT 1 FROM Table2 where SSN = t1.SSN and City = 'New York' )
Fosco
Or `SELECT SUM(t1.Payments) from Table1 t1 JOIN Table2 t2 on t1.SSN = t2.SSN WHERE t2.City = 'New York'` if you prefer to keep your ON clauses for join criteria only.
Will A
Thanks for the answer. There are indeed multiple records in Table2 for SSN. I am trying something like this from the 2nd example:SELECT SUM(Database1.Table1.Payments)FROM Database1.Table1 T1JOIN Database2.Table2 T2WHERE EXISTS( SELECT 1 FROM T2.Table2.Social = t1.SS and T2.City = 'New York')I am getting this errors:Msg 102, Level 15, State 1, Line 2Incorrect syntax near '-'.Msg 102, Level 15, State 1, Line 4Incorrect syntax near '='.I forgot to say also that the tables are in two different databases.Thanks for helping me out
Laziale
@Will - the argument about what belongs on a join clause never ceases to entertain me. I tend to keep everything on the joins because it leads to more consistency when I do left joins.
Alex Humphrey
@user362479 if you're using the exists you wouldn't have a Join
Fosco
I am still getting the same errors.
Laziale
4 part qualified name should be Database.Owner.Table.Column
Fosco
select SUM(t1.Payments) from Database1.dbo.Table1 t1 where exists (select 1 from Database2.dbo.Table2 where SSN = t1.SSN and City = 'New York')
Fosco
the point of the 't1' after the table name in the FROM clause is to alias it... then everywhere else in the query you just use 't1'..
Fosco
@Alex Humphrey: When using ANSI-92 syntax, WHERE vs ON can have a significant impact on the result. In Fosco's first query, the t2 join will only return rows where the city value is New York BEFORE the join.
OMG Ponies
@OMG Ponies - I don't understand - do you mean 'BEFORE the *where*'? What is the significant impact on the result - wouldn't it return the same result as if you used a where? Is the impact that Fosco's query (my preferred method) is more efficient in some DBMSs?
Alex Humphrey
Thanks a lot guys for all your comments. Just one small favor, now the field payments is nvarchar.When I am doing SELECT SUM(Convert(decimal(18,2), Payments))it says: Error converting data type nvarchar to numeric.AM I doing something wrong?Thanks
Laziale
all solved, I was forgetting ISNUMERIC Thanks guys
Laziale