Grails投影按组和计数

Grails投影按组和计数,grails,group-by,projection,Grails,Group By,Projection,下面是我的两门课 class Users { String emailAddress String password // String filename String firstName String lastName Date dateCreated Date lastUpdated } 及 我想运行一个与此类似的查询,本质上我想获得所有用户的列表以及他们编写的文档的数量 SELECT author_id, COUNT(Shar

下面是我的两门课

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

我想运行一个与此类似的查询,本质上我想获得所有用户的列表以及他们编写的文档的数量

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)
}
如果我去掉
count
countDistinct
我会得到不同作者的列表,但我想要的是作者和文档的计数。因此,当我将count或countDistinct子句添加到此条件时,我只得到long的数组!! 就像[2,3,4]我想要的是[author1,2],[author2,3]…] 我怎样才能做到我已经看到的, , ,
但是没有一个答案能解决我的问题

你就不能用countBy*-like吗

    SharedDocuments.countByAuthorAndReceiver(user, receiver)

你就不能用countBy*-比如

    SharedDocuments.countByAuthorAndReceiver(user, receiver)

如果您希望得到查询中描述的结果,则需要作者id或任何其他特定属性(如电子邮件)


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

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

如果您希望得到查询中描述的结果,则需要作者id或任何其他特定属性(如电子邮件)


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

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

带有分页的投影有一个错误,它只返回投影块中的最后一个字段。当前的Grails2.1.5已经修复了这个错误。这里是报告的错误

带有分页的投影有一个错误,它只返回投影块中的最后一个字段。当前的Grails2.1.5已经修复了这个错误。这里是报告的错误

无论我做什么,我的结果集都没有其他信息。在您给我的代码中打印sharedDocumentsInstanceList会产生“[2,]”输出。我做的事情与此完全相同,但使用命名查询,没有maxResults,对我来说效果很好:(.BTW我甚至没有做author.id只是对author进行分组应该可以工作,无论我做什么,我的结果集只是计数没有其他信息。在您给我的代码中打印sharedDocumentsInstanceList会产生“[2,]”输出我正在做一些完全相同的事情,但是使用命名查询,没有maxResults,它对我来说很好:(.顺便说一句,我甚至没有做author.id,只是对author进行分组也应该有效