SQL 查询抛出“不在聚合函数或 group by 子句中” 例外
我正在修复我们项目的测试套件,该项目正在通过 Hibernate/DBUnit 进行测试。 有几个测试用例都从 Hibernate 抛出类似的异常,看起来像这样:
java.sql.SQLException: Not inaggregate function or group by Clause: org.hsqldb.Expression@109062e in statements [.. .blah ...]
通过我的谷歌搜索,我怀疑这是由于我们使用聚合函数 AVG() 引起的,因为它出现在异常消息中,并且抛出的所有查询都包含它。 但是,我发现了一些指向遇到此错误的人员的链接,并且能够通过注释掉“ORDER BY”或“GROUP BY”子句或通过在分组中包含 SELECT 子句中的其他列来修复该错误。 我明白为什么这会修复这样的错误消息,但我不确定它是否适用于我的情况,因为我尝试做同样的事情,但没有什么区别。 此外,我们有一些测试用例抛出使用 ORDER/GROUP 的异常,但不是全部。 例如:
ThingerVO myThinger = (ThingerVO)session.createQuery("SELECT new ThingerVO(" +
"r.id, " + "u.id, " + "u.alias, " + "s.id, " +
"s.name, " + "r.URL," + "AVG(v.rating), " +
"r.totalCount, " + "r.isPrivate, " + "a.id, " +
"a.name, " + "r.transactionId, " + "r.size, " +
"u.hasPicture " +
") FROM Thinger r LEFT OUTER JOIN r.votes as v, Table1S s " +
"JOIN s.Table2A AS a, User u " +
"WHERE r.userId = u.id AND " +
"s.id = r.Table1SId AND " +
"r.id = :thingId")
.setInteger("thingId", thingId)
.uniqueResult();
此查询也会导致抛出相同的异常,即使它不使用 ORDER/GROUP 子句。 另外,我将 Hibernate 生成的 HSQL 代码直接剪切/粘贴到 MySQL 查询浏览器中,并且运行没有问题。 另外,值得指出的是,所有这些代码在我们的生产数据库上都运行良好,所以我真的很困惑为什么它会在这里抛出。
其他一些可能有用的信息——我们使用平面 XML 数据库结构,其中包含一些用于测试用例的虚拟测试数据,以及用于 hibernate 的 MySQL 方言。 我们使用 dbunit 2.4.3/hibernate 3.2.6。 我尝试使用最新的 hibernate 版本 3.3.1,但它的行为是一样的。
任何指示或提示将不胜感激。
I'm working on repairing the test suite for a project of ours, which is being tested through Hibernate/DBUnit. There are several test cases which all throw a similar exception from Hibernate, which looks something like this:
java.sql.SQLException: Not in aggregate function or group by clause: org.hsqldb.Expression@109062e in statement [... blah ...]
Through my googling, I am suspicious that this is caused by our use of the aggregate function AVG(), as this is in the exception's message, and all of the queries that throw contain it. However, I discovered several links to people who were getting this error, and were able to fix it by either commenting out an "ORDER BY" or "GROUP BY" clause, or by including the other columns from the SELECT clause in the grouping. I understand why this would fix such an error message, but I'm not sure whether it applies to my situation, because I tried doing the same and it made no difference. Also, we have some test cases throwing exceptions which use ORDER/GROUP, but not all. For example:
ThingerVO myThinger = (ThingerVO)session.createQuery("SELECT new ThingerVO(" +
"r.id, " + "u.id, " + "u.alias, " + "s.id, " +
"s.name, " + "r.URL," + "AVG(v.rating), " +
"r.totalCount, " + "r.isPrivate, " + "a.id, " +
"a.name, " + "r.transactionId, " + "r.size, " +
"u.hasPicture " +
") FROM Thinger r LEFT OUTER JOIN r.votes as v, Table1S s " +
"JOIN s.Table2A AS a, User u " +
"WHERE r.userId = u.id AND " +
"s.id = r.Table1SId AND " +
"r.id = :thingId")
.setInteger("thingId", thingId)
.uniqueResult();
This query also causes the same exception to be thrown, even though it doesn't use an ORDER/GROUP clause. Also, I cut/pasted the generated HSQL code from Hibernate directly into the MySQL query browser, and it ran without problems. Also, it's worth pointing out that all of this code works fine on our production database, so I'm really confused as to why it throws here.
Some other potentially useful information -- we're using a flat XML database structure with some dummy test data for the test cases, and the MySQL dialect for hibernate. We're using dbunit 2.4.3/hibernate 3.2.6. I tried using the latest hibernate, version 3.3.1, but it behaved the same.
Any pointers or hints would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在 SQL 查询的 SELECT 部分中使用聚合函数(例如
AVG()
)以及其他非聚合表达式,那么您必须有一个 GROUP BY 子句,该子句应列出所有非聚合函数。聚合表达式。我对 java 不熟悉,但是看看代码,看起来它将创建并运行一个类似这样的查询(不太正确,但我认为足够接近):
... 这没有
GROUP BY
,但在 SELECT 子句中混合聚合和非聚合表达式。 问题是 SQL 格式错误。解决方法是在查询末尾添加一个
GROUP BY
。我无法说出为什么这在您的生产系统中起作用,但我怀疑其中存在一些细微的差异。 也许有什么东西会自动添加
GROUP BY
?你能发布它执行的 SQL 的打印输出吗?
If you use an aggregate function (e.g.
AVG()
) in the SELECT part of the SQL query along with other non-aggregate expressions, then you must have a GROUP BY clause which should list all the non-aggregate expressions.I'm not familiar with java, but looking at the code, it looks like it's going to create and run a query something like this (not quite right, but close enough, I think):
... This has no
GROUP BY
, but does mix aggregate and non-aggregate expressions in the SELECT clause. The problem is that the SQL is badly formed.The fix would be to add a
GROUP BY
to the end of the query.I can't say why this is working in your production system, but I suspect that there is some subtle difference there. Perhaps something is adding the
GROUP BY
automatically?Can you post a printout of the SQL it executes?
此外,当 order-by 字段不是字符串时,
ORDER BY
在 hsqldb 中不起作用。不幸的是,这会导致 Not inaggregate function or group by Clause 错误消息,这表明存在分组问题,因此会造成混乱......
请参阅:http://markmail.org/message/42vmifme4opz4jgl
Also,
ORDER BY
does not work in hsqldb when the order-by field is not a String.Unfortunately, this results in the Not in aggregate function or group by clause error message, which suggests a grouping problem, hence the confusion...
See: http://markmail.org/message/42vmifme4opz4jgl
在某些系统(例如 TALEND)中,如果有注释行,查询将不起作用 示例:
给出 MS SQL 查询错误。 而不是注释行
使用这些符号进行注释
也许它会帮助某人并节省一些时间。
In some systems (for example TALEND) query doesn't work if there are comment lines example:
Gives an error for MS SQL queries. Instead of comment line
use these symbols for commenting
Maybe it will help someone and save some time.