Solved

SQL - Join Statment with SUM

Posted on 2013-02-04
4
363 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

776 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