我有一个 grails 域类,它看起来像这样:
class Answer {
String response
Integer score
Date created = new Date()
}
如何进行查询以返回一周中每一天的平均分数?, 像这样 :
average for Mondays = 25
average for Tuesdays = 20
....
average for sundays = 10
我有一个 grails 域类,它看起来像这样:
class Answer {
String response
Integer score
Date created = new Date()
}
如何进行查询以返回一周中每一天的平均分数?, 像这样 :
average for Mondays = 25
average for Tuesdays = 20
....
average for sundays = 10
要获得星期天的平均值,首先要获取星期天的日期。然后使用条件查询来查找平均值。请参见下面的示例:
dateOfSunday = ..
def c = Answer.createCriteria()
def avgScore = c.get {
projections {
avg "score"
}
lt("created",dateOfSunday.plus(1) )
gt("created",dateOfSunday.minus(1) )
}
希望你有这个想法...
编辑:
好的,如果您想要所有星期天的平均值,那么您可以在域类中使用公式,例如:
class Answer {
String response
Integer score
Date created = new Date()
static mapping = {
dayOfWeek formula: 'DAYOFWEEK(created)'
}
}
然后查询将是:
def c = Answer.createCriteria()
def avgScore = c.get {
projections {
avg "score"
}
eq("dayOfWeek ",7)
}
编辑:
@dmahapatro,感谢您指出。
所有平均分数也可以使用单个 db hit 获得。见下文:
def results = Answer.createCriteria().list() {
projections {
groupProperty('dayOfWeek')
avg "score"
}
}
虽然它涉及的代码较少,但它有一个缺点,它使用了 db 函数,这会使查询变慢。您可以使用 dmahapatro 的建议。
这是@lucke84 答案的补充,是对他的方法的一个小修改(以防我对他的答案的编辑被遗漏)
def summation = [
'monday': [],
'tuesday': [],
'wednesday': [],
'thursday': [],
'friday': [],
'saturday': [],
'sunday': []
]
def answers = Answer.list()
answers.each {
def dateCreated = it.created[Calendar.DAY_OF_WEEK]
switch (dateCreated) {
case Calendar.MONDAY:
summation.monday << it.score
break
case Calendar.TUESDAY:
summation.tuesday << it.score
break
case Calendar.WEDNESDAY:
summation.wednesday << it.score
break
case Calendar.THURSDAY:
summation.thursday << it.score
break
case Calendar.FRIDAY:
summation.friday << it.score
break
case Calendar.SATURDAY:
summation.saturday << it.score
break
case Calendar.SUNDAY:
summation.sunday << it.score
break
}
}
def averages = [:]
averages.monday = (summation['monday'].sum())?.divide(summation['monday'].size())
//So on and So forth.
记下可能会影响性能.list()
的所有行。Answer
在这种情况下,您可以根据需要使用findAll
和传递参数。pagination
我不确定我是否有您的问题和您的域模型,但也许这份草稿可能会有所帮助。
def summations = [
'monday': [],
'tuesday': [],
'wednesday': [],
'thursday': [],
'friday': [],
'saturday': [],
'sunday': []
]
def answers = Answer.list()
answers.each {
def dateCreated = it.created[Calendar.DAY_OF_WEEK]
switch (dateCreated) {
case Calendar.MONDAY:
summations['monday'] << it.score
break
case Calendar.TUESDAY:
summations['tuesday'] << it.score
break
case Calendar.WEDNESDAY:
summations['wednesday'] << it.score
break
case Calendar.THURSDAY:
summations['thursday'] << it.score
break
case Calendar.FRIDAY:
summations['friday'] << it.score
break
case Calendar.SATURDAY:
summations['saturday'] << it.score
break
case Calendar.SUNDAY:
summations['sunday'] << it.score
break
}
}
// you can now do the average for every list in the map (sum of elements divided by number of item)
编辑:这是来自@dmahapatro:
def averages = [:]
averages.monday = (summations['monday'].sum())?.divide(summations['monday'].size())
// So on and So forth.