SQL Server AVG 函数的奇怪之处
我在 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中加权平均计算的结果
I am seeing some odd behavior in SQL Server AVG calcuation.
On manual calculation, you get 49.277588
but SQL Server is reporting that the average is 50.9914
as shown below.
Question: Can someone explain the difference and why this is happening?
You can try out the query on AdventureWorks2008 Database with following query
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
[Update] Answer
Here is the result of Weighted Average calculation in Excel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在 Excel 中计算平均值,这是一个糟糕的数学计算。
http://wiki.answers.com/Q/Is_an_average_of_averages_accurate
It looks like you're doing an average of an average in Excel, which is bad math.
http://wiki.answers.com/Q/Is_an_average_of_averages_accurate