grailsgrails-ormcreatecriteria

Grails minimum value fetch


I am working to fetch the minimum value from a table in Grails where the vehicleid and the type is given

there are many values associated from that vehicleid and type i need to fetch the minimum date associated with that vehicleid . I have tried this but it is not working for the startDate param.

List vehicleDate= Table.createCriteria().list() {
   and{
      eq('vehicleid',vehicleIDGiven)
      eq('type',type)
      min("startDate")
   }
}

what could be the query like


Solution

  • If you just need to fetch the minimum value, you could use a projection as documented here.

    Not tested, but your criteria should look something like this:

    def result = Table.createCriteria().list() {
        and{
            eq('vehicleid',vehicleIDGiven)
            eq('type',type)
        }
        projections {
            min("startDate")
       }
    }
    
    println "Minimum start date is ${result[0]}"