Solved

MS Access 2003 - sum is producing too many decimal places

Posted on 2011-03-14
6
755 Views
Last Modified: 2012-05-11
SELECT tblDrafts.D_CaseNumber, Sum(tblDrafts.D_Amount) AS SumOfD_Amount
FROM tblDrafts
GROUP BY tblDrafts.D_CaseNumber;

produces sum with many decimal digits instead of two digits as defined in the table. By using Round function, it is not giving correct sum in decimal values. Example, the value in table is say 169531.10 and it is showing 169531.09 because it is rounding 169531.09354...
0
Comment
Question by:gpdixit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35128633
try this

SELECT tblDrafts.D_CaseNumber, formatnumber(Sum(tblDrafts.D_Amount),2) AS SumOfD_Amount
FROM tblDrafts
GROUP BY tblDrafts.D_CaseNumber;
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 35128832
gpdixit,

Unless you are using the Decimal data type for that column with a Scale parameter of two, then you are NOT actually storing your values as numbers with two decimal places--rather, they are merely formatted that way for display.  As such, cap1's suggestion will not necessarily return the correct answer.

Instead, you would have to round each value BEFORE it goes into the sum:

SELECT D_CaseNumber, Sum(Round([D_Amount], 2)) AS SumOfD_Amount
FROM tblDrafts
GROUP BY D_CaseNumber;

Open in new window


That, of course, uses "banker's rounding".  To round Excel-style:

SELECT D_CaseNumber, Sum(Val(Format([D_Amount], "0.00"))) AS SumOfD_Amount
FROM tblDrafts
GROUP BY D_CaseNumber;

Open in new window


Be warned, though, that taking the sum of rounded items is a mathematically dubious idea :)

Patrick
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 35129558
Always - always - use data type Currency for currency, amounts, etc.

If you can 't change the fields of the table, convert to Currency at the earliest stage:
SELECT 
  tblDrafts.D_CaseNumber, 
  Sum(CCur(tblDrafts.D_Amount)) AS SumOfD_Amount
FROM 
  tblDrafts
GROUP BY 
  tblDrafts.D_CaseNumber;

Open in new window


The reason for your rounding errors is caused by adding positive and negative amounts when these are Single or Double.

/gustav
0
Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 35129815
I was wondering when your "Spidey sense" would start tingling, gustav :)
0
 
LVL 50

Expert Comment

by:Gustav Brock
ID: 35129915
Believe me, it's operating at random!

/gustav
0
 

Author Closing Comment

by:gpdixit
ID: 35153893
Expert gave two solutions, out of which second solution was acceptable. I tried and and it is successful. Solution was provided in a very short time. Lot of thanks.
0

Featured Post

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

717 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