Solved

SQL - Join Statment with SUM

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

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 39

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
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.
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

708 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

16 Experts available now in Live!

Get 1:1 Help Now