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 回答