具有相同值的SQLite计算结果不同

时间:2015-06-30 10:50:02

标签: sql sqlite math

我的表格包含此值

productName | purchasePrice | tax | price 
------------+---------------+-----+--------
Product 1   | 5099          | 16  | 10099
Product 1   | 5099          | 16  | 10099
Product 1   | 5099          | 16  | 10099
Product 1   | 5099          | 16  | 10099
Product 2   | 5099          | 19  | 10099
Product 2   | 5099          | 19  | 10099
Product 2   | 5099          | 19  | 10099

我的总利润计算是这个

SUM( price- ( price * tax/100.0  + purchasePrice)) as Profit

结果为利润= 22780.210000000006

我对每件产品的利润计算是

SUM(price- (price*TAX/100.0 + purchasePrice)) as Profit GROUP BY productName

结果为产品1的利润= 13536,6
结果为产品2的利润= 9243,57

22780,17

我必须对这个值进行舍入并将它们除以/100.0,因为我已经读过,最好不要在Sqlite中存储浮点值。

我像这样进行舍入和/100.0

总计,结果为= 227.8

ROUND((SUM( price- ( price * tax/100.0  + purchasePrice)))/100 ,2) as Profit

每个产品的回合

ROUND((SUM( price- ( price * tax/100.0  + purchasePrice)))/100 ,2) as Profit GROUP BY productName

产品1结果= 135,37
产品2结果= 92,44

227,81 ,但总计的回合给出结果= 227.8

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

在最新版本的SQL-lite中,以下代码适用于Round

select SUM(Profit) from (
select productName,round((SUM(price- (price*TAX/100.0 + purchasePrice)))/100 ,2)
as Profit
from test
GROUP BY productName
)x

http://sqlfiddle.com/#!5/8eba1/25