There are various ways to achieve this. Those include namedQueries, criteria, detached criteria or even HQL. Here is the namedQuery which would get what is needed:
static namedQueries = {
maxRevision {
eq 'revision', {
projections {
max 'revision'
}
}
//projections if needed
projections {
property 'name'
}
}
}
//controller
AppInfo.maxRevision().list()
With a detached criteria, it would be similar as:
AppInfo.withCriteria {
eq 'revision', {
projections {
max 'revision'
}
}
projections {
property 'name'
}
}
with HQL:
select ai from AppInfo as ai
where ai.revision = (
select max(revision) from AppInfo
)
taking into consideration this below domain class:
class AppInfo {
String name
Integer revision
}
UPDATE
Above would give the max of all the revisions. If you are looking for max of each group then you will have to use the below HQL:
AppInfo.executeQuery("""
select ai from AppInfo as ai
where ai.revision in (
select max(a.revision) from AppInfo as a
where a.name = ai.name
)
""")
This can also be written as a Criteria.