Question

I am using a named query with projections in my domain class. I would like the named query to return one of the values using the following equation:

sum(x * y) / sum(y)

I am familiar with formulas in mapping and attempted to do the following:

def formVar

static mapping = {    
    formulaVar formula: 'sum(x * y) / sum(y)'
}

Any help here would be awesome.

Was it helpful?

Solution 2

OK. Got this working like this:

class SumThing {
    Double sumVar

    static namedQueries = {
        sums { op ->
            projections {
                property "sumVar"
            }
        }
    }

    static mapping = {
        version false
        columns {
            sumVar formula: 'SUM(X * Y) / SUM(Y)'//X and Y being actual column names from your database
        }
    }
}

OTHER TIPS

I might be misunderstanding your question, but you may want to check out the beforeUpdate and beforeInsert events.

Code placed in this block will allow you to preform calculations and manipulate the domain class properties before inserting/updating them.

It is documented in the Grails manual here http://grails.org/doc/2.3.7/guide/GORM.html#eventsAutoTimestamping

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top