Question

I have this table for the orders

+-----+--------+--------+-----------+-----------+
|   id|order_id|   price|        tax|       qty |
+-----+--------+--------+-----------+-----------+
|    5|     123|  231.40|      50.91|          1|
+-----+--------+--------+-----------+-----------+
|    6|     123|  210.74|      46.36|          1|
+-----+--------+--------+-----------+-----------+

and this query for getting the results for the order 123

SELECT Sum(price * qty)               AS tot_sub,
       ( Sum(( price + tax ) * qty) ) AS tot
FROM   products
WHERE  order_id = 123  

and this is the results

+-------+-----------------+
|tot_sub|tot              |
+-------+-----------------+
|442.14 |539.4100000000001|
+-------+-----------------+

I'm expecting a perfect value for tot (539.41)

I know that the internal representation of floating point numbers (in general) can have some errors, and differs from the exact theoretical number.

In this case, can I write a better query for avoiding this error without using the ROUND function?

thanks

Was it helpful?

Solution

Use cast as decimal.

Change:

SELECT Sum(price * qty) AS tot_sub,
       ( Sum(( price + tax ) * qty) ) AS tot
FROM   products
WHERE  order_id = 123  

To:

SELECT Sum(price * qty) AS tot_sub,
       Sum( ( cast( price as decimal(10,2) ) 
            + cast( tax   as decimal(10,2) ) ) * qty ) AS tot
FROM   products
WHERE  order_id = 123  

Refer to:

  • MySQL: CAST(expr AS type)
    • The CAST() function takes an expression of any type and produces a result value of a specified type.

OTHER TIPS

SELECT Sum(price * qty)               AS tot_sub,
       Round( Sum(( price + tax ) * qty) ),2) AS tot
FROM   products
WHERE  order_id = 123  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top