Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Aggregate query not correct
Gary Becker 
     
3 years ago
I have a problem with a query. If I run it without the aggregate function it returns 776 which is the correct number. If I group them on the RID (which is the size & length combined) I get 653. I've copied the data into Excel and summarized it--and Excel gives me the correct result. I've tried to analyze which lines were incorrect, but there isn't any rhyme or reason that I can see. Any idea as to why Access is doing this?
Gary Becker OP  @Reply  
     
3 years ago

Gary Becker OP  @Reply  
     
3 years ago

Gary Becker OP  @Reply  
     
3 years ago

Gary Becker OP  @Reply  
     
3 years ago

Kevin Yip  @Reply  
     
3 years ago
You seem to have negative values in your table that you want to filter out, but this is not how you do in an aggregate query.  Your method only checks if the *total sum* is greater than zero, which it always is in your case.  So your method includes all the negative values in your sum as well, and that's why you get a lower sum, 653 instead of 776.  You need to check for positive values *before* the sum function is performed.

In the query designer, instead of writing:

     Quantity
     Sum
     >0

write this:

     IIf([Quantity] < 0, 0, [Quantity])
     Sum

Then each and every row of data will go through the IIf() function before it is being summed, and all the negative values will be treated as zeroes, essentially ignored.

The actual SQL statement should clarify this as well.  Your initial method has something like this:

     HAVING Sum( ... ) > 0

indicating it is the total sum that is being check for positivity.

But my method has this:

     Sum(IIf([Quantity] < 0, 0, [Quantity]))
Gary Becker OP  @Reply  
     
3 years ago
Kevin, thanks for your help. I wasn't able to get the iif to work--but what you said about filtering out the negative quantities gave me the idea to add a where condition to the query. "Where [Quantity]>0" is working. I assumed that if I had a query that had the correct totals--then grouping it would also give me the correct totals. I learned something new. I hope I remember it always. Here is the SQL.

SELECT tblPilingInventory.Job, [Size] & [length] AS RID, tblPilingInventory.Size, tblPilingInventory.Length, Sum(tblPilingInventory.Quantity) AS Received
FROM tblPilingInventory
WHERE (((tblPilingInventory.Quantity)>0))
GROUP BY tblPilingInventory.Job, [Size] & [length], tblPilingInventory.Size, tblPilingInventory.Length
HAVING (((tblPilingInventory.Job)="c1187"))
ORDER BY tblPilingInventory.Job, tblPilingInventory.Size, tblPilingInventory.Length;

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 10:09:46 AM. PLT: 1s