Skip to content
Advertisement

SQL SUM Returning Incorrect Result

I’m trying to get the SUM from the columns returned by a subquery but the value I’m getting back is not what I’m expecting.

When I run this query:

I get the following:

Query Result

When I try to SUM the repair_order_amount_totals though, the number I’m getting is 2373.36 when it should be 3,675.26

Any idea why I might be getting a different value than I’m expecting when attempting to get the SUM?

Thanks!

Advertisement

Answer

To find where the problem is, you should look for suspicious coincidences between what you’re expecting, what you get, and the data.

In this case, 3675.26 – 2373.36 = 1301.90, which is really close the value “1,302.90” in the data. Thanks to user Barmar’s comment, we see that MySQL is parsing the string into a number – but only up to the comma.

The bodge method to fix it would be to remove the comma:

The correct method would be to use the DECIMAL type for all money amounts in the database, and use the UI to format the numbers with commas for display.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement