我正在使用Grails标准(类似于休眠标准)来获得学生在给定表格的每个分区中成绩最高的学生列表。我只想要Name
,Division
和Grade
字段。是否可以在Projections,Criteria,Grails中使用聚合函数和属性?
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)
只有当你想选择的列是'String'类型时,这个解决方案才会起作用。如果列是'numeric(int,long,float)'类型,它将不会工作。 –
为什么?如果可以应用聚合函数,那么它将起作用 – injecteer
我刚刚测试了它,它给出了错误的值。你也可以测试它。 –