1

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.

Community
  • 1
  • 1
Jonathan Lebrun
  • 1,462
  • 3
  • 20
  • 42

2 Answers2

4

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]}'"
}
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
2

If you like criteria queries take a look at this: stackoverflow->Using groupProperty and countDistinct in Grails Criteria

Community
  • 1
  • 1
Medrod
  • 986
  • 7
  • 17