Grails按日期分组 [英] Grails group by date
本文介绍了Grails按日期分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有日期属性的域类。
类Transaction {
LocalDate time
BigDecimal amount
}
code>
如何查询按月分组的所有交易的总和?我无法在GORM中找到对日期范围的任何支持。
解决方案
在您的域中添加基于公式的字段类的截断日期:
$ p $ 类Transaction {
LocalTime时间
BigDecimal金额
字符串timeMonth
static mapping = {
timeMonth formula:FORMATDATETIME(time,'yyyy-MM')// h2 sql
// timeMonth formula:DATE_FORMAT(time, '%Y-%m')// mysql sql
}
}
<然后你就可以运行这样的查询了:
Transaction.withCriteria {
projections {
sum('amount')
groupProperty('timeMonth')
}
}
I have a domain class with a date-property.
class Transaction {
LocalDate time
BigDecimal amount
}
How can I query for the sum of all transactions grouped by month? I can´t find any support for group by a date-range in GORM.
解决方案
Add a formula based field to your domain class for the truncated date:
class Transaction {
LocalTime time
BigDecimal amount
String timeMonth
static mapping = {
timeMonth formula: "FORMATDATETIME(time, 'yyyy-MM')" // h2 sql
//timeMonth formula: "DATE_FORMAT(time, '%Y-%m')" // mysql sql
}
}
Then you'll be able to run queries like this:
Transaction.withCriteria {
projections {
sum('amount')
groupProperty('timeMonth')
}
}
这篇关于Grails按日期分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文