MySQL SUM() Doesn’t Play Well With Floats
I had to write some reports for some legacy software today and I was unpleasantly surprised with the results of my SQL queries. I was selecting dollar values and summing them to for the monthly spending of certain individuals. Easy enough right? I wrote a query something like this :
SELECT SUM(t.money_spent) as sum_of_spent, c.customer_name from transactions t join customers c on t.customer_id=c.customer_id group by customer_name order by c.customer_name asc
I ended up getting numerical values that were 10 decimal places long with seemingly random numbers. After checking to make sure the database didn’t have any odd entries I stumbled on this bug report.
The ‘money_spent’ column had a data type of float, which is a waste, but I still don’t think that it should sum up incorrectly. When I select individual values I get proper two decimal results.
Apparently floats and doubles use floating point math, which deals with approximate values for numbers and can thus result in confusion like this. It seems that it isn’t really possible to store 0.1 in a column of type float. You can only store 0.00999999977648258. This behavior is a little silly but easily fixed by using the ROUND() function :
SELECT SUM(ROUND(t.money_spent)) as sum_of_money_spent, c.customer_name from transactions t join customers c on t.customer_id=c.customer_id group by customer_name order by c.customer_name asc