Solved

Left Join and Sum()

Posted on 2010-09-17
3
399 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TSQL Challenge... 7 35
Nested Case statement 4 37
Error when creating an UPDATE Trigger in SQL 6 18
SQL Quer 4 21
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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

808 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