如何避免在此 JPA/JPQL 查询中重复函数调用?
How to avoid duplicating a function call in this JPA/JPQL query?
我有以下 JPA 查询将日期截断为一个完整小时并计算它们:
SELECT a.alertconfiguration.id.id,
date_trunc('hour', a.date) AS fromDate,
count(*) AS count
FROM alert a
GROUP BY a.alertconfiguration.id.id,
a.alertlevel,
date_trunc('hour', a.date)
我 运行 在使用 Hibernate 的 Spring 启动应用程序中。它工作正常。但我不想重复对 date_trunc
的函数调用。
我曾尝试在 GROUP BY 子句中引用 fromDate,但随后出现异常 org.postgresql.util.PSQLException: ERROR: column "fromdate" does not exist
http://hibernate.atlassian.net/browse/HHH-9301 还声明不可能在 group by 子句中引用别名。
如何在没有重复函数调用的情况下重写我的查询?
Hibernate 不适用于 group by 中的别名或任何聚合函数。正如您将在生成的 sql 查询中看到的那样,别名与您分配的别名不同。
你能否尝试在 group by 子句中使用 2 而不是 date_trunc('hour', a.date),因为 fromdate 是第二列
我有以下 JPA 查询将日期截断为一个完整小时并计算它们:
SELECT a.alertconfiguration.id.id,
date_trunc('hour', a.date) AS fromDate,
count(*) AS count
FROM alert a
GROUP BY a.alertconfiguration.id.id,
a.alertlevel,
date_trunc('hour', a.date)
我 运行 在使用 Hibernate 的 Spring 启动应用程序中。它工作正常。但我不想重复对 date_trunc
的函数调用。
我曾尝试在 GROUP BY 子句中引用 fromDate,但随后出现异常 org.postgresql.util.PSQLException: ERROR: column "fromdate" does not exist
http://hibernate.atlassian.net/browse/HHH-9301 还声明不可能在 group by 子句中引用别名。
如何在没有重复函数调用的情况下重写我的查询?
Hibernate 不适用于 group by 中的别名或任何聚合函数。正如您将在生成的 sql 查询中看到的那样,别名与您分配的别名不同。
你能否尝试在 group by 子句中使用 2 而不是 date_trunc('hour', a.date),因为 fromdate 是第二列