带有小数的MS Access SQL Average函数

时间:2015-02-12 20:08:16

标签: sql ms-access

我正在尝试为商品创建评级系统,并在表单上显示该商品的所有评级的平均值。我希望评级有1位小数。我想出了下面的代码:

SELECT OvrRate.OvRating, *
FROM tbl1 LEFT JOIN 
    (SELECT Field1, ROUND(AVG(Rating),1) AS OvRating 
     FROM tbl_Review GROUP BY Field1)  AS OvrRate 
ON tbl1.Field1 = OvrRate.Field1;

到目前为止,我的代码工作在显示1位小数,但当它应该是5.3时,它显示5.0。所有评论都妥善存储(我认为)。我有一篇关于5.3的评论,这意味着平均值也应该是5.3,但就像我上面说的那样,它不是这样查询的。

任何修复?

1 个答案:

答案 0 :(得分:1)

MS Access对整数执行整数运算,因此您需要转换该值。我发现最简单的方法是乘以1.0:

SELECT OvrRate.OvRating, *
FROM tbl1 LEFT JOIN 
    (SELECT Field1, ROUND(AVG(Rating*1.0), 1) AS OvRating 
     FROM tbl_Review
     GROUP BY Field1
    )  AS OvrRate 
    ON tbl1.Field1 = OvrRate.Field1;