AVG()在SQL Server中不正确 [英] AVG() not accurate in SQL Server

查看:178
本文介绍了AVG()在SQL Server中不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SQL Server Management Studio 2008为where条件的列计算Avg().当我在SQL查询中尝试AVG(column-name)时,它为我提供了一个四舍五入的值,而不是十进制值.

I am trying to calculate an Avg() for an column with where condition using SQL Server Management Studio 2008. When I try the AVG(column-name) in a SQL query it gives me a rounded value and not a decimal value.

当我将数据集复制到MS Excel中时,在SQL中我得到的准确值是4位小数(例如:10.74),我得到的只有10位.

When I copy the dataset into MS Excel I get the accurate value with 4 decimal (example: 10.74) in SQL I am getting just 10. Any help is appreciated.

我的查询:

SELECT Item, AVG(POOrdrQty) 
FROM [tableWR] 
where Item ='737' AND POOrdrQty <((select AVG([POOrdrQty]) FROM [tableWR]) * 2) 
group by Item

推荐答案

avg聚合的结果类型与您用作参数值的类型相同.如果该字段为int,则结果将四舍五入以适合该类型.

The resulting type of the avg aggregate is the same type as the value you use as parameter. If the field is an int, the result will be rounded to fit the type.

保留用作参数的值:avg(cast(column-name as float)).

这篇关于AVG()在SQL Server中不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆