AVG() not accurate in SQL Server

我与影子孤独终老i 提交于 2019-12-13 23:50:03

问题


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.

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.

My query:

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

回答1:


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.

Cast the value that you use as parameter: avg(cast(column-name as float)).



来源:https://stackoverflow.com/questions/18493976/avg-not-accurate-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!