伙计们! 我尝试使用 JPA (EclipseLink + Derby) 在 JPQL 中按时间戳对查询结果进行分组,并使用它们创建 Result 对象。 我的查询是:
Select new com.restaurant.entities.Report(o.timeOfOrder, count(o.id), sum(o.price))";
queryText+=" from Orders o ";
queryText+="where o.timeOfOrder BETWEEN :start AND :end";
queryText+=" group by o.timeOfOrder"
不幸的是,只有一毫秒就会产生巨大的差异,因此简单的 group by o.timeOfOrder 不起作用。
我已经尝试过this approach ,但是我有
Internal Exception: java.sql.SQLSyntaxErrorException: Column reference 'ORDERS.TIMEOFORDER' is invalid, or is part of an invalid expression. For a SELECT list with a GROUP BY, the columns and expressions being selected may only contain valid grouping expressions and valid aggregate expressions.
也许,除了改变DB中字段的类型之外,还有其他出路吗?
最佳答案
您的查询必须如下所示:
Select new com.restaurant.entities.Report(o.timeOfOrder, count(o.id), sum(o.price))
from Orders o
where o.timeOfOrder BETWEEN :start AND :end
group by year(o.timeOfOrder), month(o.timeOfOrder), day(o.timeOfOrder)
但 year
、month
和 day
函数不是 jpql 标准,因此您必须使用 FUNCTION 来使用等效的 DB 函数来执行你想要什么。更多信息如下:https://stackoverflow.com/a/3676865/534877
关于java - 使用 JPA 在 jpql 中按时间戳进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33454061/