Postgres计数没有groupby(使用jpa)

时间:2016-05-01 14:49:07

标签: java postgresql jpa

我一直在使用mariaDB,现在我正在转向postgres。 我在JPA中有一个查询

select count(<primarykeycolumn>) from tablename where condition

但是Postgres给出了一个错误,说明需要group by条款。

我不想添加组,代码已启动并与mariaDB一起运行。

以下是错误:

Internal Exception: org.postgresql.util.PSQLException: ERROR: column "app_movie_summary.creationdate" must appear in the GROUP BY clause or be used in an aggregate function
  Position: 55
Error Code: 0
Call: SELECT COUNT(id) FROM mytable ORDER BY CREATIONDATE DESC
Query: ReportQuery(name="App_Movie_Summary.findAllByCreationDateCount" referenceClass=App_Movie_Summary sql="SELECT COUNT(Id) FROM mytable ORDER BY CREATIONDATE DESC")
    at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:340)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:684)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:560)
    at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2055)
    at org.eclipse.persistence.sessions.server.ServerSession.executeCall(ServerSession.java:570)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:299)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:694)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2740)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllReportQueryRows(ExpressionQueryMechanism.java:2677)
    at org.eclipse.persistence.queries.ReportQuery.executeDatabaseQuery(ReportQuery.java:852)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:904)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1134)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:460)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1222)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1857)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1839)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:258)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getSingleResult(QueryImpl.java:521)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:400)

1 个答案:

答案 0 :(得分:0)

在PostgreSQL中,除非count未聚合列,否则不能将聚合函数(如creationdates)与非聚合列(GROUP BY)混合使用。在这种情况下,看起来你不需要分组,因为你不需要ORDER BY creationdate。订单与count(*)无关。此查询完全有效:

SELECT count(*) FROM mytable

如果您确实需要creationdate,则必须 GROUP BY。当你这样做时,ORDER BY也有意义:

SELECT creationdate, count(*) from mytable
GROUP BY creationdate
ORDER BY creationdate