获取select语句的AVERAGE()

时间:2013-02-24 14:18:34

标签: sql-server tsql average

如何获得select查询结果的平均值

示例:

select x from dbo.table1 as a
where MONTH(a.mymonth) = 1 AND YEAR(a.myyear) = 2013

是否可以使用AVG()功能?

3 个答案:

答案 0 :(得分:3)

这就是你要找的全部吗?

select AVG(x)
from dbo.table1
where MONTH(mymonth) = 1 AND YEAR(myyear) = 2013

Sample Fiddle Demo

根据您的x数据类型,您可能希望将其强制转换为小数:

SELECT AVG(CAST(x as decimal))

答案 1 :(得分:0)

是否可以使用AVG()函数?

是的,将它包裹在x周围,所以:

    select AVG(x) from dbo.table1 as a
    where MONTH(a.mymonth) = 1 AND YEAR(a.myyear) = 2013

此处了解更多信息。

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_avg

答案 2 :(得分:0)

如果您希望平均x您的select查询应如下所示:

Select AVG(x)
from dbo.table1 as a
where MONTH(a.mymonth) = 1 AND YEAR(a.myyear) = 2013