gpt4 book ai didi

java - 如何在HQL中进行除法、计算百分比?

转载 作者:行者123 更新时间:2023-11-30 04:16:19 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

关于java - 如何在HQL中进行除法、计算百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18291488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com