我正在尝试计算百分比,但编译器一直告诉我它不期望斜杠和第三个选择语句。
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
我做错了什么? 提前谢谢!
答案 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