grails查询以获取一周中每天的平均值

时间:2013-05-24 09:27:46

标签: grails

我有一个grails域类,看起来像这样:

class Answer {
String response
Integer score
Date created = new Date()
}         

如何进行查询以返回一周中每天的平均分数? ,像这样:

average for Mondays = 25
average for Tuesdays = 20
....
average for sundays = 10

3 个答案:

答案 0 :(得分:2)

要获得星期日的平均值,请先获取星期日的日期。 然后使用条件查询来查找平均值。见下面的例子:

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,谢谢你指出来。

所有平均分数也可以使用单个数据库命中。见下文:

def results = Answer.createCriteria().list() {
  projections {
    groupProperty('dayOfWeek')
    avg "score"
  }
} 

虽然它涉及的代码较少,但它有一个缺点,它使用的是db函数,这会使查询变慢。您可以使用dmahapatro的建议。

答案 1 :(得分:1)

我不确定我是否有您的问题和您的域名模型,但也许草稿可能会有所帮助。

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.

答案 2 :(得分:1)

这是对@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个参数。