Home > Blockchain >  How can I get the maximum value of a specific day in Gorm?
How can I get the maximum value of a specific day in Gorm?

Time:04-12

I have written following code to get the daily maximum of a certain value with GORM.

  1. I pass the current time and get the day's start and end.
  2. I select all values between the day's start and end.
  3. I order the temperatures and get the first.

My Code:

func GetDailyMaxTemperature(ts time.Time) (*Temperature, error) {
    temp:= &Temperature{}
    start, end := getStartAndEndOfDay(ts)
    if tx := db.Where("ts BETWEEN ? AND ?", start, end).Order("temperature ASC").First(temp); tx.Error != nil {
        return temp, tx.Error
    }
    return temp, nil

func getStartAndEndOfDay(ts time.Time) (time.Time, time.Time) {
    dayStart := time.Date(ts.Year(), ts.Month(), ts.Day(), 0, 0, 0, 0, ts.Location())
    dayEnd := time.Date(ts.Year(), ts.Month(), ts.Day(), 23, 59, 59, 999, ts.Location())
    return dayStart, dayEnd
}

This code works, however I am not very satisfied with it and wonder if there are more "GORM-ish" ways to get a maximum value of a certain table especially when there are dates involved.

CodePudding user response:

You could use the max operation in SQL. Maybe not a more 'GORM' way to do this, but in my opinion a more semantically correct/appealing version:

var result float64
row := db.Table("temperatures").Where("ts BETWEEN ? AND ?", start, end).Select("max(temperature)").Row()
err := row.Scan(&result)

You could make this a method of the Temperature struct like this:

func (t Temperature) MaxInRange(db *gorm.DB, start, end time.Time) (float64, err) {
  var result float64
  row := db.Table("temperatures").Where("ts BETWEEN ? AND ?", start, end).Select("max(temperature)").Row()
  err := row.Scan(&result)
  return result, err
}
  • Related