2

このGORMオブジェクトがあるとしましょう

Class Person {
  String country
  int age
  String Name
}

各国の最年長者を取得したい

この方法で投影を取得できますが、 Person オブジェクトのリストが必要です。

def results = Person.withCriteria {
  projections {
    groupProperty("country")
    max('age')
 }
}
4

1 に答える 1

0
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 "+
            ")"
         )
}

うまくいけば、これがお役に立てば幸いです。国ごとに最大年齢の人のリストがある可能性があるため、これによりリストのリストが返されます。

于 2012-10-02T11:56:09.833 に答える