Grails投影分组和计数

时间:2012-04-19 05:18:46

标签: grails group-by projection

以下是我的两个班级

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?, 但没有一个答案似乎解决了我的问题!

3 个答案:

答案 0 :(得分:0)

难道你不能只使用countBy * - 比如

    SharedDocuments.countByAuthorAndReceiver(user, receiver)

答案 1 :(得分: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)
}

答案 2 :(得分:0)

分页预测有一个错误,它只返回投影块中的最后一个字段。当前的grails 2.1.5修复了这个bug。以下是报告的错误http://jira.grails.org/browse/GRAILS-9644

相关问题