我有以下查询
select sum(r.score)/count(r) from Rating as r
group by r.item
order by sum(r.score)/count(r) desc
我的问题是hibernate舍入结果总和(r.score)/ count(r)。
如何强制Hibernate返回结果的浮点值?
答案 0 :(得分:3)
如果您只想找到一个平均值,那么您可以使用avg()聚合函数,该函数始终返回Double
。
select avg(r.score) from Rating as r
group by r.item
order by avg(r.score) desc
答案 1 :(得分:1)
avg
最适合这种情况。对于要转换为double的一般情况,您可以使用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