Solved

SQL - Join Statment with SUM

Posted on 2013-02-04
4
364 Views
Last Modified: 2013-02-04
I've attached a pic of an excel spreadsheet with a table definition.  I've been trying to write a SQL statement what will perform a SUM on TableF.Amount where TableG_ID <> 2.

However my sum gives me a result of 25 (adding the 10 + 15 together).  

I tried to make the tables similar to what I am working with.  Table G will have many rows of data and I need result to sum but exclude where TableG_ID = 2.

Thanks
Tables.png
0
Comment
Question by:CipherIS
  • 2
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 38852764
write your sum like this:  


sum (
    case when  TableG_ID <> 2 then TableF.Amount else 0 end
   ) as TableAMountFexcludingG2
0
 
LVL 1

Author Comment

by:CipherIS
ID: 38852817
That got me closer but now I'm somehow getting a total of 30.  Seems that it is adding 15 twice.  But when I run a select statement it shows as

0
0
15

but in sum I get 30.

Any ideas?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 38852885
can you post your query?
0
 
LVL 1

Author Closing Comment

by:CipherIS
ID: 38852963
Thanks
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
SQL Server / Update DB? 22 38
Negative isnull? 3 16
Testing for Null in Stored Proc Return Value 5 15
MS SQL Conditional WHERE clause 3 19
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

820 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