如何创建日期字段+ Grails的分组标准 [英] how to create Criteria of Group By Month of Date Field + Grails

查看:129
本文介绍了如何创建日期字段+ Grails的分组标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望从今天到365天之后,按月收集用户数。我正在使用Hibernate Criteria构建器API的groovy。任何简单的方法来做这个分组?我们如何指定按月分组日期字段的日期格式?

I want to get count of users grouped on a month basis from today to 365 days back. I am using groovy with Hibernate Criteria builder API . Any easy way to do this grouping? how do we specify the date format for grouping the date field by month?

现在我有以下内容:

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

            }
        }

groupProperty('sunsetdate')按日期对其进行分组,甚至在分组中包含时间..因此计数被计算为了一个非常独特的约会&计数1与源表相同的时间。
如何使用此方法在分组中指定日期格式?或者我必须使用HQL?

The groupProperty('sunsetdate') groups it on a date basis and even includes the time in the grouping .. so the counts are cacluated for a very unique date & time which makes counts 1 same as the source table. How do we specify date formats in grouping using this approach? or do I have to use HQL?

在此先感谢。

Thanks in Advance.

推荐答案

您可以访问此此处了解更多详情

you can visit this here for further details


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

Create three new numeric fields each for week,month and year in the domain class. These fields won't be mapped to column in the table. Provide static mapping for the three fields.

      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')
  }
}

这篇关于如何创建日期字段+ Grails的分组标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆