Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Pull sums from 2 tables
Message
 
To
15/10/2007 11:14:08
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01261037
Message ID:
01261044
Views:
16
>I have 2 sql server 2005 tables:
>
>table1
>
>id
>owed
>paid
>
>table2
>
>id
>tpaid
>
>I want to pull:
>
> sum(table1.owed) as owed, sum(table1.paid) as paid, sum(table2.tpaid) as tpaid
> where table1.id = table2.id
>
>There are multiple records for each id in both tables. It seems simple, but I just can't seem to get it right. Any ideas?
SELECT Table1.Id, 
       SUM(Owned) AS Owned,
       SUM(Paid)  AS Paid,
       SUM(ISNULL(Table2.TPaid,0)) AS TPaid
FROM Table1
LEFT JOIN Table2 ON Table1.Id = Table2.Id
GROUP BY Table1.Id
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform