在日期字段上使用 max 的 JPQL 查询

发布于 2024-11-09 09:55:26 字数 1900 浏览 3 评论 0原文

我需要查询以从一组记录中查找具有最新日期的记录。我尝试了很多东西,最近的一个是这样的:

select msg, msg.createdDate from ImportedMessage msg where msg.siteId = ?1 and msg.createdDate = max(msg.createdDate) group by msg.createdDate

不幸的是,我尝试的一切都产生了某种错误。我似乎得到最多的错误是:

Caused by: java.sql.SQLException: Not in aggregate function or group by clause: 
org.hsqldb.Expression@688c688c in statement [select importedme0_.IMPORTED_MSG_ID as 
col_0_0_, importedme0_.CREATED_DATE as col_1_0_, max(importedme0_.CREATED_DATE) as 
col_2_0_, importedme0_.IMPORTED_MSG_ID as IMPORTED1_1_, importedme0_.CREATED_BY as
CREATED2_1_, importedme0_.CREATED_DATE as CREATED3_1_, importedme0_.UPDATED_BY as
UPDATED4_1_, importedme0_.UPDATED_DATE as UPDATED5_1_, importedme0_.IMPORT_TYPE as
IMPORT6_1_, importedme0_.MESSAGE as MESSAGE1_, importedme0_.PROCESSED_FLAG as
PROCESSED8_1_, importedme0_.SITE_ID as SITE9_1_ from IMPORTED_MSG importedme0_ where
importedme0_.SITE_ID=? and importedme0_.CREATED_DATE=max(importedme0_.CREATED_DATE) 
group by importedme0_.CREATED_DATE]
    at org.hsqldb.jdbc.Util.throwError(Unknown Source)
    at org.hsqldb.jdbc.jdbcPreparedStatement.(Unknown Source)
    at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1616)
    at org.hibernate.loader.Loader.doQuery(Loader.java:717)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    ... 52 more

我相信这告诉我的是我的 select 子句中没有适当的内容来允许该组工作。然而,我尝试了各种组合,所有的组合都会导致这个错误。

有人可以告诉我我在这里做错了什么吗?

I need to query to find a record with the most recent date from a group of records. I've tried a bunch of stuff, with the most recent being something like this:

select msg, msg.createdDate from ImportedMessage msg where msg.siteId = ?1 and msg.createdDate = max(msg.createdDate) group by msg.createdDate

Unfortunately, everything I've tried has yielded some sort of error. The error I seem to get most is:

Caused by: java.sql.SQLException: Not in aggregate function or group by clause: 
org.hsqldb.Expression@688c688c in statement [select importedme0_.IMPORTED_MSG_ID as 
col_0_0_, importedme0_.CREATED_DATE as col_1_0_, max(importedme0_.CREATED_DATE) as 
col_2_0_, importedme0_.IMPORTED_MSG_ID as IMPORTED1_1_, importedme0_.CREATED_BY as
CREATED2_1_, importedme0_.CREATED_DATE as CREATED3_1_, importedme0_.UPDATED_BY as
UPDATED4_1_, importedme0_.UPDATED_DATE as UPDATED5_1_, importedme0_.IMPORT_TYPE as
IMPORT6_1_, importedme0_.MESSAGE as MESSAGE1_, importedme0_.PROCESSED_FLAG as
PROCESSED8_1_, importedme0_.SITE_ID as SITE9_1_ from IMPORTED_MSG importedme0_ where
importedme0_.SITE_ID=? and importedme0_.CREATED_DATE=max(importedme0_.CREATED_DATE) 
group by importedme0_.CREATED_DATE]
    at org.hsqldb.jdbc.Util.throwError(Unknown Source)
    at org.hsqldb.jdbc.jdbcPreparedStatement.(Unknown Source)
    at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:452)
    at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1616)
    at org.hibernate.loader.Loader.doQuery(Loader.java:717)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
    at org.hibernate.loader.Loader.doList(Loader.java:2449)
    ... 52 more

I believe that what this is telling me is that I don't have the appropriate things in my select clause to allow the group to work. However, I've tried all sorts of combinations, and everything leads back to this error.

Can someone give me a clue what I'm doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2024-11-16 09:55:26

好吧,我猜主持人没有费心去阅读将答案向上移动的编辑:
提问者对查询意图的评论:

“我有一个表,其中包含数据元素列表(id、消息(字符串)、siteId(字符串)、createdDate(时间戳)。我需要做的是按 siteId 选择,然后找到该组中具有最新创建日期的记录。”

解决方案:

Query query = entityManagerReference.createQuery(
"SELECT msg FROM ImportedMessage msg " 
+ "WHERE msg.siteId = :siteId ORDER BY msg.createDate desc");

query.setParameter("siteId", 12345);
query.setMaxResults(1);

Well I guess the moderator didn't bother to read the edit that moved the answer up:
comment from questioner on intent of query:

"I have a table that conains a list of data elements (id, message(string), siteId (string), createdDate (Timestamp). What I need to do is select by the siteId, then find the record in that group with the most recent createdDate."

Solution:

Query query = entityManagerReference.createQuery(
"SELECT msg FROM ImportedMessage msg " 
+ "WHERE msg.siteId = :siteId ORDER BY msg.createDate desc");

query.setParameter("siteId", 12345);
query.setMaxResults(1);
江南烟雨〆相思醉 2024-11-16 09:55:26

使用 Oracle10 方言的 Hibernate 将其转换为 WHERE ROWNUM <= 1。

Hibernate using the Oracle10 dialect translate this to WHERE ROWNUM <= 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文