Solved

SQL - Join Statment with SUM

Posted on 2013-02-04
4
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

705 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