简体   繁体   中英

Grails Domain HQL Query

I have a grails domain named A. Like the following

Class A { 
    Integer x; 
    Collection<B> items 
}

and Class B is like the followings

Class B {
    Integer a;
    Integer b;
}

I need to find out sum of a*b group by x value of class a. For Example A class has rows having values x = 5, 6, 5; and some list of items.

I need two rows where i can find all the sum of items group by x means 5 and 6.... How can I do This. Thanks in advance..

*I need to do this with a single executequery like this "select count(o.id), sum(o.items.a * o.items.b) from A as o group by ax" Is there any way i can do that like this....*

try this:

def query = "FROM B AS b HAVING SUM(t.a * t.b) NOT NULL"
def result = A.findAll(query) 

Try to this if this working:

def aList = A.createCriteria()
def result = aList.list() {
    projections {
        sum('a * b') != null
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM