How to count on different values in the same query with Grails?
I have a class with a status :
class A{
String status
}
This status can have values 'start','end','inprogress'.
I would like get the number of 'start','end' and 'inprogress' in one query.
I see 开发者_运维技巧this post : Different record count values in one query but It's only for Oracle.
Is it possible to do that in Grails/GORM ?
Thanks.
You can use executeQuery
:
def counts = A.executeQuery(
'select status, count(status) from A group by status')
This will return a List of Object[], e.g.
for (row in counts) {
println "there are ${row[1]} with status '${row[0]}'"
}
If you like criteria queries take a look at this: stackoverflow->Using groupProperty and countDistinct in Grails Criteria
精彩评论