2012-04-19 63 views
2

以下是我的兩個班Grails的預測由集團和計​​數

class Users { 
    String emailAddress 
    String password 
    // String filename 
    String firstName 
    String lastName 
    Date dateCreated 
    Date lastUpdated 
} 

class SharedDocuments { 
    Users author 
    Users receiver 
    Documents file 
    static constraints = { 

    } 
} 

我想運行與此類似的查詢,基本上是我想要得到所有用戶的列表隨着文件數量以及他們所撰寫

SELECT author_id, COUNT(SharedDocuments.id)FROM SharedDocuments 
    INNER JOIN users ON author_id = users.id 
    GROUP BY author_id 

這是我迄今爲止

def sharedDocumentsInstanceList = SharedDocuments.createCriteria().list(params){ 
    createAlias("author","a") 
    eq("receiver.id",session.uid) 
    projections{ 
     groupProperty "author" 
     count "id",'mycount' 

    } 
    order('mycount','desc') 
    maxResults(params.max) 
} 

我有這個90%的工作,如果我擺脫countcountDistinct我得到不同的作者名單,但我想要的是與文檔的計數一起作者。所以當我添加count或countDistinct子句到這個標準,我只是得到數組長! 像[2,3,4]我要的是[author1,2],[author2,3] ...] 我怎樣才能實現這一目標我已經看到Grails: Projection on many tables?Grails criteria projections - get rows countGrails groupProperty and order. How it works?, 但沒有的答案似乎解決了我的問題!

回答

0

你不能只是使用countBy * - 像

SharedDocuments.countByAuthorAndReceiver(user, receiver) 
0

如果你想要的結果,你在查詢中所描述,那麼你需要作者或任何其他特定的屬性,如電子郵件的ID。


def sharedDocumentsInstanceList = SharedDocuments.createCriteria().list(params){ 
    eq("receiver.id",session.uid) 
    projections{ 
     groupProperty "author" 
     count "id",'mycount' 

    } 
    order('mycount','desc') 
    maxResults(params.max) 
} 
+0

無論我做什麼我的結果集只是沒有其他信息計數。在你給我的代碼中打印sharedDocumentsInstanceList生成'[2,]'輸出 – Sap 2012-04-19 14:49:20

+0

我做了一些完全一樣的事情,但用命名查詢,沒有maxResults,它對我來說工作正常:(。順便說一句,我甚至沒有做author.id只是分組在作者身上也應該工作 – 2012-04-21 05:38:30