2

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

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

我究竟做错了什么?提前致谢!

4

1 回答 1

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
于 2013-08-18T19:39:57.597 回答