2

我想统计从今天到 365 天前按月分组的用户数。我正在将 groovy 与 Hibernate Criteria builder API 一起使用。有什么简单的方法来做这个分组吗?我们如何指定按月对日期字段进行分组的日期格式?

现在我有以下内容:

             def con_cnt =Users.createCriteria().list {
            like('employeeid','c_%')
            between('sunsetdate', fromDate, toDate)
            projections {
                count('employeeid')
                //groupProperty('sunsetdate')

            }
        }

groupProperty('sunsetdate') 以日期为基础对其进行分组,甚至将时间包括在分组中.. 因此计数是针对非常独特的日期和时间计算的,这使得计数 1 与源表相同。我们如何使用这种方法在分组中指定日期格式?还是我必须使用 HQL?

提前致谢。

4

1 回答 1

3

您可以在此处访问以获取更多详细信息

在域类中分别为周、月和年创建三个新的数字字段。这些字段不会映射到表中的列。为三个字段提供静态映射。

      static mapping = {

    week formula('WEEK(DATE_OF_EXPENSE)')    //provide the exact column name of the date field
    month formula('MONTH(DATE_OF_EXPENSE)')
    year formula ('YEAR(DATE_OF_EXPENSE)')

    }
    def results = c.list {
  between("dateOfExpense", fromDate, toDate) 
  projections {
    switch(groupBy){
        case "week":
           groupProperty('year')
           groupProperty('month')
           groupProperty('week') 
        break;
        case "month"
           groupProperty('year')
           groupProperty('month')
        break;
        case "year":
           groupProperty('year')
        break;
    }        
    sum('amount')
  }
}
于 2013-03-25T15:29:38.887 回答