我有一个域类(最小化):-
class Expense {
Date dateOfExpense
int amount
}
我试图获取按支出日期的周/月/年分组的金额总和。
在grails doc http://grails.org/doc/latest/guide/GORM.html中引用“sqlGroupProjection”方法,
我尝试使用以下代码:-
def results = c {
between("dateOfExpense", fromDate, toDate)
projections {
sqlGroupProjection 'dateOfExpense,sum(amount) as summed',
'MONTH(dateOfExpense)',['date','summed'],[DATE,NUMBER]
}
}
引发异常:
No such property: DATE for class: grails.orm.HibernateCriteriaBuilder. Stacktrace follows:
Message: No such property: DATE for class: grails.orm.HibernateCriteriaBuilder
请提出一种使用
sqlGroupProjection
方法的方法
最佳答案
static mapping = {
//provide the exact column name of the date field
week formula('WEEK(DATE_OF_EXPENSE)')
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 - 在GORM中按周/月/年分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14083310/