我在下面有一个查询,我希望在同一gorm查询中的2个日期之间计数

    def view = StackHash.createCriteria().list(params) {
        if(params?.process){
            eq("process",params.process)
            between("lastDateOfOccurence", fromDate, upToDate)
        }
        order("occurrence", "desc")
    }

    [
        title              : title,
        filter             : CommonService.makeFilterParams(params),
        subtitle           : subtitle,
        view               : view,
        reportInstanceTotal: StackHash.count() ///this is getting total number of records in the database irrespective of dates
    ]

最佳答案

如果您想知道符合条件的记录总数,可以使用结果的totalCount属性:

def view = StackHash.createCriteria().list(params) {
        if(params?.process){
            eq("process",params.process)
            between("lastDateOfOccurence", fromDate, upToDate)
        }
        order("occurrence", "desc")
    }

    [
        title              : title,
        filter             : CommonService.makeFilterParams(params),
        subtitle           : subtitle,
        view               : view,
        reportInstanceTotal: view.totalCount
    ]

请注意,view.totalCount不一定与view.size()相同。 .size()会告诉您结果集中有多少条记录,.totalCount会告诉您有多少条记录符合搜索条件。这些数字可能有所不同。例如,如果您的数据库中有1,000条记录,其中500条记录符合您的条件,并且params包含类似[max: 10, offset: 0]的内容,则.size()将为10,而.totalCount将为500。

希望对您有所帮助。

关于grails - 我还如何获取Grails中2个日期之间的记录计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56703352/

10-14 12:54
查看更多