このGORMオブジェクトがあるとしましょう
Class Person {
String country
int age
String Name
}
各国の最年長者を取得したい
この方法で投影を取得できますが、 Person オブジェクトのリストが必要です。
def results = Person.withCriteria {
projections {
groupProperty("country")
max('age')
}
}
このGORMオブジェクトがあるとしましょう
Class Person {
String country
int age
String Name
}
各国の最年長者を取得したい
この方法で投影を取得できますが、 Person オブジェクトのリストが必要です。
def results = Person.withCriteria {
projections {
groupProperty("country")
max('age')
}
}
def countries = Person.executeQuery(
"select distinct person.country from Person person")
def persons = countries.collect{ country ->
Person.executeQuery(
"from Person as person1 " +
"where person1.country='${country}' and person1.age=" +
"("+
"select max(age) "+
"from Person as person2 "+
"where person2.country='${country}' "+
"group by person2.country "+
")"
)
}
うまくいけば、これがお役に立てば幸いです。国ごとに最大年齢の人のリストがある可能性があるため、これによりリストのリストが返されます。