views:

2196

answers:

1

Hi, I have the following query which id like to sorty by "raceDate" and by "raceNo" asc. I can figure out how to sort by one field, but not by two, any ideas?

def list = {
  params.max = Math.min( params.max ? params.max.toInteger() : 20,  100) 
        params.offset = params?.offset?.toInteger() ?: 0
        params.sort = "raceDate"
        params.order = params?.order ?: "asc"

        def results = Race.createCriteria().list(
             max: params.max,
                offset: params.offset,
                sort: params.sort, 
                order: params.order
             ) {
            and {
                if (params.raceNo && params.raceNo != '')
                    eq("raceNo", params.raceNo.toInteger())
                if (params.country && params.country != '')
                    eq("country", params.country)
                if (params.venue && params.venue != '')
                    eq("venue", params.venue)
                if (params.raceType && params.raceType != '')
                    eq("raceType", params.raceType)
                if (params.surface && params.surface != '')
                    eq("surface", params.surface)
                if (params.officialGoing && params.officialGoing != '')
                    eq("officialGoing", params.officialGoing)

                if (params.raceDateStart_year && params.raceDateStart_month && params.raceDateStart_day 
                        && params.raceDateEnd_year && params.raceDateEnd_month && params.raceDateEnd_day) {
                    String startInput = "${params.raceDateStart_year}/${params.raceDateStart_month}/${params.raceDateStart_day}"
                    Date startDate = jodaFormatter.parseDateTime(startInput).toDate();
                    String endInput = "${params.raceDateEnd_year}/${params.raceDateEnd_month}/${params.raceDateEnd_day}"
                    Date endDate = jodaFormatter.parseDateTime(endInput).toDate();  

                    between("raceDate", startDate, endDate)
                }
            }
        }

        [ raceInstanceList:results, raceInstanceTotal:results.totalCount, params:params ]
}
+1  A: 

Something like this should work:

def results = Race.createCriteria().list(
                max: params.max,
                offset: params.offset,
                sort: params.sort, 
                ) {
                order('raceDate', 'asc')
                order('raceNo', 'asc')
                and {
                    if (params.raceNo && params.raceNo != '')
                        eq("raceNo", params.raceNo.toInteger())
                ...
Dave Klein
This does not work, and throws the error:Column "table.RaceNo" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause.
nerdgerl