简体   繁体   English

SQL Server AVG函数异常

[英]SQL Server AVG function oddity

I am seeing some odd behavior in SQL Server AVG calcuation. 我在SQL Server AVG计算中看到一些奇怪的行为。

On manual calculation, you get 49.277588 通过手动计算,您将获得49.277588
but SQL Server is reporting that the average is 50.9914 但SQL Server报告的平均值为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 您可以使用以下查询尝试在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

替代文字

[Update] Answer [更新]答案
Here is the result of Weighted Average calculation in Excel 这是Excel中加权平均计算的结果 替代文字

It looks like you're doing an average of an average in Excel, which is bad math. 看来您正在Excel中进行平均,这是不好的数学运算。

http://wiki.answers.com/Q/Is_an_average_of_averages_accurate http://wiki.answers.com/Q/Is_an_average_of_averages_accurate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM