如何进行划分,计算HQL中的百分比?

时间:2013-08-17 17:24:18

标签: java hibernate hql

我正在尝试计算百分比,但编译器一直告诉我它不期望斜杠和第三个选择语句。

Query query = session.createQuery("SELECT 
(SELECT COUNT(*) FROM Reservation r WHERE r.begin BETWEEN :start AND :end) 
/
(SELECT COUNT(*) FROM Reservation res WHERE res.begin BETWEEN :start AND :end AND res.contract IS NOT NULL) * 100");

当我尝试运行它时,它给了我这个错误:

Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree

我做错了什么? 提前谢谢!

1 个答案:

答案 0 :(得分:2)

两个计数都可以合并为一个查询:

SELECT (count(r.contract) / count(r)) * 100 
FROM Reservation r 
WHERE r.begin between :start and :end

在一些导致整数除法的数据库中,后续更好:

SELECT (count(r.contract)*1.0 / count(r)) * 100 
FROM Reservation r 
WHERE r.begin between :start and :end