Solved

Query to return sum of values where the sum <> 0

Posted on 2013-11-18
8
319 Views
Last Modified: 2013-11-18
I need to return the sum of fields grouped by accountid. I only want to see the results where the resulting sum is not equal to 0.

The query below works except I get a bunch of rows with a value of 0 which I do not want. What am I missing? Basic SQL but I am stuck.

Select Distinct
  ledger.accountid,
  Round(Sum(ledger.amount), 2) As BalanceDue
From
  ledger
Where
  ledger.glaccountid = 1
Group By
  ledger.accountid

Open in new window

0
Comment
Question by:ckelsoe
  • 5
  • 3
8 Comments
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 39657377
Add this -
having Round(Sum(ledger.amount), 2) > 0
0
 

Author Closing Comment

by:ckelsoe
ID: 39657384
Ah - forgot about having. I did <>0 and it is what I need. Thanks
0
 
LVL 32

Expert Comment

by:awking00
ID: 39657398
Assuming you could have negative sums, just change to -
having Round(Sum(ledger.amount), 2) <> 0
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

Author Comment

by:ckelsoe
ID: 39657402
Can you do an and in having? When I try I get no results

For example,
having (Round(Sum(ledger.amount), 2) < -1
and Round(Sum(ledger.amount), 2) > 1)
0
 
LVL 32

Expert Comment

by:awking00
ID: 39657405
Sorry, I didn't see your last post where you already figured it out :-)
0
 
LVL 32

Expert Comment

by:awking00
ID: 39657413
Can you do an and in having? When I try I get no results

No (a sum can't be both plus and minus), but you could do OR
having (Round(Sum(ledger.amount), 2) < -1
OR Round(Sum(ledger.amount), 2) > 1)
0
 

Author Comment

by:ckelsoe
ID: 39657416
Yes - that is what I needed.  Thanks and have a great rest of the day!
0
 
LVL 32

Expert Comment

by:awking00
ID: 39657434
Thanks, you do the same. Not sure mine will be that great, I have a dentist appointment :-(
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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