我正在使用Grails 标准(类似于休眠标准)从给定表中获取在每个部门中获得最高成绩的学生列表。我只想要Name
,Division
和Grade
字段。
Name | Division | Grade | Std_id
---------------------------------
AA1 | A | 2 | 1
AA2 | A | 4 | 2
BB1 | B | 2 | 3
BB2 | B | 5 | 4
我想要的结果是
Name | Division | Grade |
--------------------------
AA2 | A | 4 |
BB2 | B | 5 |
如果我使用以下标准
def criteria = Student.createCriteria()
def resultlt = criteria.list {
projections {
groupProperty('divison')
max('grade')
}
}
我只有Division
和Grade
,不包括其他字段。我也需要Name
领域。
如果我将标准(在预测中一起使用聚合函数和属性)更改为
def criteria = Student.createCriteria()
def resultlt = criteria.list {
projections {
property('name')
groupProperty('divison')
max('grade')
}
}
它给出以下错误..
ERROR: column "this_.name" must appear in the GROUP BY clause or be
used in an aggregate function
Position: 63. Stacktrace follows:
org.postgresql.util.PSQLException: ERROR: column "this_.name" must
appear in the GROUP BY clause or be used in an aggregate function
Position: 63
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryEx
ecutorImpl.java:2161)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutor
Impl.java:1890)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.ja
va:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Stat
ement.java:559)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(Abstract
Jdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc
2Statement.java:302)