# PRB： 部门使用的公式中使用 SUM 或 AVG 函数时，丢失精度的可能出现

##### 替代方法

• 用作除数的数字。例如：

``DECLARE @Quantity numeric(15,9)SET @Quantity = 3SELECT Round(CAST(Sum(t1.Amount) AS numeric(15,9))/@Quantity, 9) As CostFROM t1					``
• 第一次，在变量中存储总和并在公式中使用该变量。例如：

``declare @quantity numeric (15,9)set @quantity = 3declare @sum numeric (15,9)set @sum= (select sum(amount) from t1)select @sum/@quantity as cost					``
• 将显式值放在一个分母中。例如：
``select sum(Amount)/3 from t1					``
##### 更多信息

``CREATE TABLE t1 (    Amount    numeric(15,9)    )GOINSERT INTO t1( Amount ) VALUES (0.7)INSERT INTO t1( Amount ) VALUES (1.3)SELECT Amount FROM t1/* result (correct, scale = 9)Amount      ----------------- .7000000001.300000000DECLARE @Quantity numeric(15,9)SET @Quantity = 3    SELECT t1.Amount / @Quantity As CostFROM t1				``

``DECLARE @Quantity numeric(15,9)<BR/>SET @Quantity = 3SELECT Sum(t1.Amount)/@Quantity As Cost<BR/>FROM t1				``

``select sum(Amount)/3 from t1GOselect 2.0000000/@quantity				``

``declare @quantity numeric (15,9)set @quantity = 3declare @sum numeric (15,9)set @sum= (select sum(amount) from t1)select @sum/@quantity as cost				``

#### 警告：本文已自动翻译

Microsoft SQL Server 7.0 标准版, Microsoft SQL Server 2000 标准版

• kbnosurvey kbarchive kbmt kbprb KB281341 KbMtzh