How to implement "select sum ()" in Grails

I have a connection like this:

class E {
  string name
  hasMany = [me:ME]
}

class M {
  float price
}

class ME {
 E e
 M m
 int quantity
}

      

And I wouldn't like to iterate to achieve this: "get the sum of the quantity in ME times when the price is the corresponding M for a given E".

Any hints on how to implement it with GORM / HQL?

Thanks in advance

+2


a source to share


1 answer


Haven't tested this, but it should be close if it doesn't work out of the box:



def givenE = // the E we're looking for
def sum = ME.executeQuery("select sum(m.price) from ME as me join me.m as m where me.e = :e", [e: givenE])
println sum

      

+8


a source







All Articles