Mit groupProperty und countDistinct in Grails-Kriterien

Bin ich mit Grails 1.2.4. Ich würde gerne wissen, wie kann ich Sortieren nach "countDistinct" (absteigend) und mit groupProperty innerhalb eines Projektionen.

Hier sind meine domains:

class Transaction {

    static belongsTo = [ customer : Customer, product : Product ]

    Date transactionDate = new Date()

    static constraints = {
        transactionDate(blank:false)    
    }

}

class Product {

    String productCode

    static constraints = {
        productCode(blank:false)    
    }
}

In der MySQL-Bedingungen, das ist, was ich will:

select 
    product_id,
    count(product_id)
from
    transaction
group by
    product_id
order by
    count(product_id) desc

Im Allgemeinen Begriff, ich würde gerne eine Liste der Produkte (oder Produkt-id) sortiert nach der Anzahl der Transaktionen, die ein Produkt hatte (absteigend)

Dies ist meine Vermutung:

def c = Transaction.createCriteria() def transactions = c.list {
    projections {
        groupProperty("product")
        countDistinct("product")
    }
    maxResults(pageBlock)
    firstResult(pageIndex) }

def products = transactions.collect { it[0] }

Aber nicht meine erwartete Ergebnis. Alle führen zu diesem wird sehr geschätzt. Danke!

InformationsquelleAutor firnnauriel | 2010-09-15

Schreibe einen Kommentar