473,413 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

SQL calculations different than on calculator?

I'm running SQL query to caluclate projected food costs. The
calculation is this:

(ReportedFoodSales / PlanFoodSales) * FullPlanFoodSales

Seems simple enough to me.

Using the following numbers, SQL comes up with a different answer than
what I do with a calulator. The data types are money. I'm sure there
is some reasonable explanation....right?

Reported Food Sales: 28096.4500
Plan Food Sales: 28608.4167
Full Plan Food Sales: 137702.0000

SQL Answer: 135237.1342
Calculator Answer: 135237.7308

Any ideas?

Thanks,
Jennifer
Jul 20 '05 #1
3 4439
je**********@hotmail.com (Jennifer) wrote in
news:33**************************@posting.google.c om:
I'm running SQL query to caluclate projected food costs. The
calculation is this:

(ReportedFoodSales / PlanFoodSales) * FullPlanFoodSales

Seems simple enough to me.

Using the following numbers, SQL comes up with a different answer than
what I do with a calulator. The data types are money. I'm sure there
is some reasonable explanation....right?

Reported Food Sales: 28096.4500
Plan Food Sales: 28608.4167
Full Plan Food Sales: 137702.0000

SQL Answer: 135237.1342
Calculator Answer: 135237.7308

Any ideas?

Thanks,
Jennifer


SQL does its calculations in a manner consistent with its currency
definition, that is it uses whole numbers mutiplied by 10000
It divides 280964500 by 286084167 and gets
0.98210433295317597915161799219738
It multiplies this by 10000 and use only the whole number part of that
answer: 9821.

It multiplies 9821 * 1377020000 and gets 13523713420000. It then divides
by (10000 * 10000) and gets 135237.1342. (note it would cut it to 4
decimals even if there were more in float).

Probably, you could emulate the calculator's results in SQL by casting
your currency values as floats before doing your calculating and then
casting the result back to currency before using or storing it. But I
find this to be a bit flaky and test various syntaxes until I get just
what I want. Sometimes I've had to use the currency as a string before I
convert it to a float, but this may be because I have missed something.
Try it on your calculator as I've described if you would like
verification.
Jul 20 '05 #2
On 8 Nov 2004 10:39:32 -0800, Jennifer wrote:
I'm running SQL query to caluclate projected food costs. The
calculation is this:

(ReportedFoodSales / PlanFoodSales) * FullPlanFoodSales

Seems simple enough to me.

Using the following numbers, SQL comes up with a different answer than
what I do with a calulator. The data types are money. I'm sure there
is some reasonable explanation....right?

Reported Food Sales: 28096.4500
Plan Food Sales: 28608.4167
Full Plan Food Sales: 137702.0000

SQL Answer: 135237.1342
Calculator Answer: 135237.7308

Any ideas?

Thanks,
Jennifer


Hi Jennifer,

Lyle has already provided an explanation. A workaround is very simple to
find in this case. Since the division loses some precision, and this
precision loss is magnified by the multiplication that follows, the
workaround is to force SQL Server to do the multiplication first (you
won't lose much rpecision there), then finish with the division:

(FullPlanFoodSales * ReportedFoodSales) / PlanFoodSales

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 20 '05 #3
Lyle & Hugo :

Thank you both for your explanations! It's appreciated.

Thanks,
Jennifer
Hugo Kornelis <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message news:<oq********************************@4ax.com>. ..
On 8 Nov 2004 10:39:32 -0800, Jennifer wrote:
I'm running SQL query to caluclate projected food costs. The
calculation is this:

(ReportedFoodSales / PlanFoodSales) * FullPlanFoodSales

Seems simple enough to me.

Using the following numbers, SQL comes up with a different answer than
what I do with a calulator. The data types are money. I'm sure there
is some reasonable explanation....right?

Reported Food Sales: 28096.4500
Plan Food Sales: 28608.4167
Full Plan Food Sales: 137702.0000

SQL Answer: 135237.1342
Calculator Answer: 135237.7308

Any ideas?

Thanks,
Jennifer


Hi Jennifer,

Lyle has already provided an explanation. A workaround is very simple to
find in this case. Since the division loses some precision, and this
precision loss is magnified by the multiplication that follows, the
workaround is to force SQL Server to do the multiplication first (you
won't lose much rpecision there), then finish with the division:

(FullPlanFoodSales * ReportedFoodSales) / PlanFoodSales

Best, Hugo

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ladvánszky Károly | last post by:
Could anybody help me on how to do precise calculations with Python the way hand-held calculators do? A small example to show the impreciseness coming from the floating point arithmetics: ...
3
by: brian kaufmann | last post by:
Hi, I had sent this earlier, and would appreciate any suggestions on this. I need to make calculations for unemployment rate for three different data sources (A,B,C) for many countries and age...
6
by: ohynes | last post by:
hey newbie here wondering if there is anyone out there who can help me im trying to make a c# subnet calulator using the IPAddress class i cant seem to figure out how to do all the subnet...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
2
by: phjones | last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
5
by: Kuldeep | last post by:
Hi All, Platform: Visual Studio 2005 Language: C#.NET Could you please help me find a pretty good component which can act as an Add-In to Visual Studio 2005 to carry out "Scientific...
4
by: papawilley | last post by:
How do I right JavaScript, quantity calculations for an existing online order form. I am trying to make an order form that will have a open price, added to a set price selected from a drop down box,...
5
Cowbie
by: Cowbie | last post by:
Hello everyone, I'm very new to PHP and I'm keen to learn how it all works. I've been looking for help all day and reading tutorials etc which is how I came accross this forum - so here's hoping...
3
by: mandy335 | last post by:
public class Calculator { private long input = 0; // current input private long result = 0; // last input/result private String lastOperator = ""; // keeps track of...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.