?
Solved

SQL Query Help with SUM

Posted on 2011-10-25
4
Medium Priority
?
258 Views
Last Modified: 2013-11-05
How do I sum up the value of a given column for the rows that meet my query criteria, then return just the primary key of that column when that sum equals zero?
0
Comment
Question by:scross1276
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
tlayton earned 2000 total points
ID: 37027976
You mean something like this?
SELECT 
	PKColumn
FROM
	TableName
WHERE
	...
GROUP BY 
	PKColumn HAVING SUM(OtherColumn) = 0

Open in new window

0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 37028002
If you mean to show the sum if non-zero, and nothing if the sum is zero...


SELECT ID, CASE WHEN SUM(OtherColumn) <> 0 THEN SUM(OtherColumn) ELSE NULL END AS Result
FROM SomeTable
GROUP BY ID

Open in new window

0
 

Author Closing Comment

by:scross1276
ID: 37028021
Um...yeah, you rock!  Thanks!
0
 
LVL 5

Expert Comment

by:tlayton
ID: 37028052
You're welcome, happy coding :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Backup & Restore 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.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

621 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