我有一个包含一些计算字段的查询。 ' Total' Column生成一个带有2个小数位的值,但Commission和Net_Receipt列有4个小数位,尽管代码看起来完全相同。任何人都可以告诉我为什么以及如何纠正后两到二DP。
SELECT `exhibition_sales`.`name`,
`exhibition_sales`.`category`,
`exhibition_sales`.`catologue_number`,
`exhibition_sales`.`title`,
`exhibition_sales`.`quantity`,
`exhibition_sales`.`unit_price`,
`exhibition_sales`.`commision_rate`,
`exhibition_sales`.quantity * `unit_price` AS `Total`,
`exhibition_sales`.quantity * `commision_rate` * unit_price AS `Commision`,
`exhibition_sales`.quantity * `unit_price` - `quantity` * `unit_price` * `commision_rate` AS `Net_Receipt`
FROM exhibition_sales
ORDER BY `exhibition_sales`.`name` ASC
答案 0 :(得分:0)
小数位输出格式的规则非常复杂。如果你想要一个特定的数字,那么只需要转换成你想要的格式:
select . . .,
cast(`exhibition_sales`.quantity * `commision_rate` * unit_price as decimal(10, 2)) AS `Commision`,
cast(`exhibition_sales`.quantity * `unit_price` - `quantity` * `unit_price` * `commision_rate` as decimal(10, 2)) AS `Net_Receipt`
documentation中有一些解释。
答案 1 :(得分:0)
假设您使用的是SQL-Server,rules of multiplication and division with numerics适用:
Total
是(x,0) - 值与(x,2) - 值的乘法,因此最终比例将为0 + 2 = 2.因此5 * 2.00 == 10.00
Commission
是(x,0) - 值,a(x,2) - 值和a(x,2) - 值的乘法,因此最终比例为(0 + 2)+ 2 = 4.因此5 * 2.00 * 4.00 == 40.0000
Net_Receipt
是两次乘法的总和;最终比例将是max(s1 + s2,s3 + s4),即max(0 + 2, 2 + 2) == 4
。因此5 * 2.00 + 1.00 * 3.00 == 13.0000