2

I have the following query

select sum(r.score)/count(r) from Rating as r
    group by r.item
    order by sum(r.score)/count(r) desc

My problem is that hibernate rounds the result sum(r.score)/count(r).

How can I force Hibernate to return the float value of the result?

4

2 回答 2

3

如果您只想找到一个平均值,那么您可以使用avg()聚合函数,它总是返回一个Double.

select avg(r.score) from Rating as r
    group by r.item
    order by avg(r.score) desc
于 2013-08-26T17:13:41.143 回答
1

avg最适合这种情况。对于要转换为双精度的一般情况,您可以使用 CAST:

select sum(r.score) / (CAST (count(r) AS DOUBLE PRECISION)) from Rating as r
    group by r.item
    order by sum(r.score) / (CAST (count(r) AS DOUBLE PRECISION)) desc
于 2013-08-26T17:26:12.313 回答