开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜