Solved

Left Join and Sum()

Posted on 2010-09-17
3
372 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
Comment Utility
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
Comment Utility
Im not sure what u mean with the first statement but coascale did it :)
0
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

744 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

13 Experts available now in Live!

Get 1:1 Help Now