问题
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