Average of subquery in GORM

穿精又带淫゛_ 提交于 2019-12-20 06:24:29

问题


I have a table T with columns A & C, from which I would like to retrieve an average count like so:

select avg(AC) as AV
from
( 
 select A, count(1) as AC
 from T
 where C = 1
 group by A
)

How do I accomplish this in GORM? GRAILS version 2.2.0 I tried following the documentation but there are no good examples. I couldn't even get the subquery to work :(

Update

I was able to get the count portion to work. Still now sure how to get the average since I don't know how to select from a query.

def tc = T.createCriteria()

def tCounts = tc.buildCriteria {
    and {
        eq 'C', 1
    }
    projections {
       groupProperty 'A'
       rowCount('AC')
    }
}

回答1:


Here is done in HQL. Tested in a grails console with one of my domains:

T.executeQuery("""
select avg(count(*))
from T t
where t.c = 1
group by t.a
""")



回答2:


UPDATE
Lately it was found that in-memory database was restricting to use avg() on count(). After numerous cross-examination, it was found that with Oracle db actually the below HQL can yield appropriate result and would eradicate the complexity of fetching the rows from db and then calculating the average in Groovy:

T.executeQuery("select avg(count(t)) from Team as t where t.c = 1 group by t.a")

The problem I was facing with in-memory db is showcased as a sample.


INIT
On my first look at the query I thought it can be achieved by using DetachedCriteria but avg projection can only be applied on domain properties but cannot be applied on count(). Same goes with HQL.

You can use combination of withCriteria and groovy collect to get the average of count of A as:

def countOfA = T.withCriteria{
    eq 'c', 1
    projections{
        count 'a'
        groupProperty 'a'
    }
}?.collect{it?.first()}

def averageCount = countOfA ? (countOfA.sum() / countOfA.size()) : 0


来源:https://stackoverflow.com/questions/17666843/average-of-subquery-in-gorm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!