【发布时间】:2009-09-07 18:09:20
【问题描述】:
我在 SQL Server AVG 计算中看到一些奇怪的行为。
手动计算,你会得到49.277588
但 SQL Server 报告平均值为 50.9914
如下图。
问题:有人可以解释其中的区别以及为什么会这样吗?
您可以使用以下查询在 AdventureWorks2008 数据库上尝试查询
select C.ProductCategoryID, P.ProductSubcategoryID,
AVG(P.ListPrice) as 'Average',
MIN(P.ListPrice) as 'Miniumum',
MAX(P.ListPrice) as 'Maximum'
from Production.Product P
join Production.ProductSubcategory S
on S.ProductSubcategoryID = P.ProductSubcategoryID
join Production.ProductCategory C
on C.ProductCategoryID = S.ProductCategoryID
where P.ListPrice <> 0
group by C.ProductCategoryID, P.ProductSubcategoryID
with rollup
[更新]答案
这是Excel中加权平均计算的结果
【问题讨论】:
标签: sql sql-server tsql excel weighted-average