Solved

Left Join and Sum()

Posted on 2010-09-17
3
384 Views
Last Modified: 2012-05-10
I though this was basic but some help are needed..

I do a left join because there MIGHT exist records in table2.

 select table1.amount, table2.prize
 from table1
 left join table2 on table1.id = table2.table1id

My problem begins here,
 select sum(table1.amount - table2.prize) as houseWin
 from table1
 left join table2 on table1.id = table2.table1id

Sum is removing all records that does not corresponds in table2.
Sum() goes totally wrong when grouping records.

WANT THIS
IF amount and prize exist the result is, do (amount-prize)
IF there aren't a prize (returns null in left join) the result is (amount-0)

Help!
0
Comment
Question by:dingir
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 33700725
Because    is always null :)In SQL Server:select sum(table1.amount - COALESCE(table2.prize, 0)) as houseWin  from table1  left join table2 on table1.id = table2.table1idAccess:select sum(table1.amount - Nz(table2.prize, 0)) as houseWin  from table1  left join table2 on table1.id = table2.table1id
0
 
LVL 1

Author Closing Comment

by:dingir
ID: 33702698
Im not sure what u mean with the first statement but coascale did it :)
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33702842
dingir,Glad to help :)What I meant was that in your original query, you have some instances where there is a number for table1.amount, but table2.prize was null.  Any time you try to perform an operation where at least one side is null, the result will be null.COALESCE worked because it substituted a non-null value, in this case zero, any time table2.prize was null.Patrick
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now