2

我被困住了,我有 2 张桌子:Sales而且SalesDetails

Sales: InvNum, Total, Discount, Nettotal 等...

SalesDetails: InvNum, ItemCode, Amount 等。

问题是。我想计算每件商品的折扣。我使用代码在视图中完成了它。

SELECT dbo.Sales.InvNum, 
       dbo.SalesDetails.Amount
           /dbo.Sales.Total*dbo.Sales.Discount AS DiscountPerItem 
FROM dbo.Sales 
   INNER JOIN dbo.SalesDetails ON dbo.Sales.InvNum = dbo.SalesDetails.InvNum

它工作正常,但问题是它没有给出准确的结果。例子 :

Sales.Total = 33,355,7.7500
Sales.Discount = 2,948.7500

SalesDetails.ItemCode = 1
SalesDetails.DiscountPerItem = 1.4744 (wrong result by SQL)

等等 (item2, 3, 4, .....) 那就是 DiscountPerItem 必须是(198 / 33,355,7.7500 * 2,948.7500) = 1.75 not = 1.4744 这个代码有什么错误。提前致谢

4

1 回答 1

0

尝试先乘以减少舍入误差

SELECT dbo.Sales.InvNum, 
       (dbo.SalesDetails.Amount*dbo.Sales.Discount)/dbo.Sales.Total 
           AS DiscountPerItem 
FROM dbo.Sales 
   INNER JOIN dbo.SalesDetails ON dbo.Sales.InvNum = dbo.SalesDetails.InvNum
于 2013-02-05T12:21:13.907 回答