473,385 Members | 1,622 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,385 software developers and data experts.

Loss of precision when using the sum() function in php?

4
Hi all,

Does anyone know how to work arround this problem of sum function in php?

The below query is giving me wrong results

SELECT
Expand|Select|Wrap|Line Numbers
  1. SUM(((kinisis_1c + kinisis_1e + ((kinisis_1c / kinisis_1b) * kinisis_1a))/(1 + fpa)) * 1.005) AS kinisis_1_total,
  2. SUM(((100c + 100e + ((100c / 100b)* 100a)) / (1 + fpa)) *1.005) AS 100ara_total,
  3. SUM(((super_1c  + super_1e + ((super_1c / super_1b)* super_1a)) / (1 + fpa)) *1.005) AS super_1_total,
  4. SUM(((95_1c + 95_1e + ((95_1c / 95_1b)* 95_1a)) / (1 + fpa)) *1.005) AS 95_1_total,
  5. SUM(((95_2c + 95_2e + ((95_2c / 95_2b)*  95_2a)) / (1 + fpa)) *1.005) AS 95_2_total,
  6. SUM(((super_2c + super_2e + ((super_2c / super_2b) * super_2a)) / (1 + fpa)) *1.005) AS super_2_total,
  7. SUM(((kinisis_2c + kinisis_2e + ((kinisis_2c / kinisis_2b) * kinisis_2a)) / (1 + fpa)) *1.005) AS kinisis_2_total
  8.  
  9. FROM kiniseis
  10. WHERE `date` BETWEEN '$_GET[date1]' AND '$_GET[date2]'
Between 2 dates given (57 records included) i have a loss of 315.04 between php , and the result calculated from excel adding up the values of each individual day (every day is a record).

I found that microsoft sql server has problem like that
http://support.microsoft.com/kb/281341
But the work around they suggest is not working in mysql-php i have tried that also making sum of a variable.

Does anyone know how to prevent this issue from happening?
Jul 1 '10 #1
3 2088
code green
1,726 Expert 1GB
I suggest it is a rounding issue caused by the number of decimal places used by the two platforms.
This will be happening where you are multiplying and dividing more than likely.
For instance * 1.005 can only produce an answer to 3 decimals
A way round? CAST
Jul 1 '10 #2
ORB30
4
@code green
Hmmmm i searched about cast but i didnt figured out how to implement this function in my application.
Can you guide me , or to tell me what exactly cast will do in my case?

Thanks :)
Jul 2 '10 #3
code green
1,726 Expert 1GB
What calculations are you doing in PHP?.
You have only posted SQL.

You need to decide what precision you want.
If doing the calculations in SQL then I would CAST all INT and float values to DECIMAL if they are being used in a calculation and also CAST the result.

This way the desired precision will be maintained.
I don't know the precision in Excel but to get the same results you need the same precision.
Jul 5 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Scott Cannon | last post by:
I am trying to query 3 tables all related by Clinet_ID. The Clients table, Monthly_Expenses table and Monthly_Income table. Each client can have 0>M instances of expenses, past due expenses, and...
2
by: Robert | last post by:
When you use an aggregate function in a SELECT statement, you cannot specify any fields you want they way you usually can in a SELECT statement. Only fields that are part of the GROUP BY clause...
6
by: pm100 | last post by:
Hi, I'm using a C header and functions under a c++ class. The problem is when I call the c func, say foo(...) directly under main there is no problem, when I use it in the class VS debugger...
4
by: crazy_jutt | last post by:
hi all, i heard that db2 ignores indexes when using any column function on the column which has index on it. but i have seen db2 using indexes even when using column function. what is the...
2
by: odavilar | last post by:
i'm trying to make an command shell or something like that, so i'm using system("cat archivo.txt"); for example, but what i want is todo something like system("cat"+variable), is there any way to do...
2
by: sharmilah | last post by:
I have the following code in my script and it's giving a syntax error at line *** when using the password function. My password in the mysql database is encrypted using the password function itself...
3
by: ncsthbell | last post by:
I am trying to run a query in access using the 'max' function. For example, I have many rows of data, one for each state and each state has a column for 'Miles'. I want to get the highest number of...
3
by: mejar sing | last post by:
select max(id) from user it produces one field, which is what I don't want; I want complete one row with(id,name, address)
3
by: matiruv | last post by:
Warning: mail() : Failed to connect to mailserver at "192.168.0.254" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\selection\Untitled-6.php on line 16...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.