GORM中子查询的平均值 [英] Average of subquery in GORM

查看:152
本文介绍了GORM中子查询的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子T,上面有A& C,我想从中检索出如下的平均数:

  select avg(AC)as AV 


)中选择A,从T
开始计数(1)为AC
,其中C = 1
组由A

我如何在GORM中完成这项工作? GRAILS 2.2.0版我尝试了下面的文档,但没有好的例子。我甚至无法让子查询工作:($ / b>

更新



我能够得到计数部分工作。现在仍然确定如何获得平均数,因为我不知道如何从查询中选择。

  def tc = T.createCriteria()

def tCounts = tc.buildCriteria {
和{
eq'C',1
}
预测{
groupProperty'A'
rowCount('AC')
}
}


<解决方案

这里是在HQL中完成的。在具有我的域的grails控制台中进行测试:

  T.executeQuery(
从T t $ b $中选择avg(count(*))
其中tc = 1
group by ta


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')
    }
}

解决方案

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
""")

这篇关于GORM中子查询的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆