This guide explains how to use the aggregate query functions in GORMX.
Calculates product of all values in a field.
total, err := gormx.Sum[Product]("price", nil)
// Returns: float64, errorCalculates the average of all values in a field.
avg, err := gormx.Avg[Product]("price", nil)
// Returns: float64, errorFinds the minimum or maximum value in a field.
min, err := gormx.Min[Product]("price", nil)
max, err := gormx.Max[Product]("price", nil)
// Returns: interface{}, errorCounts the number of distinct values in a field.
count, err := gormx.CountDistinct[Product]("category", nil)
// Returns: int64, errorGroups records by specified fields and performs aggregations.
results, err := gormx.GroupBy[Product](
[]string{"category"},
nil,
[]string{"COUNT(*) as count", "SUM(price) as sum", "AVG(price) as avg"},
)
// Returns: []GroupByResult, errorPerforms multiple aggregate operations in a single query.
aggregates, err := gormx.Aggregate[Product](
"price",
nil,
[]string{"sum", "avg", "min", "max", "count"},
)
// Returns: map[string]interface{}, errorAll aggregate functions support the Where parameter for filtering data:
// Create where conditions
where := gormx.NewWhere()
where.Set("category", "Electronics")
where.Set("price", 100, &gormx.SetWhereOptions{IsFuzzy: true}) // price > 100
// Use with aggregates
total, err := gormx.Sum[Product]("price", where)- Equal:
where.Set("field", value) - Not Equal:
where.Set("field", value, &gormx.SetWhereOptions{IsNotEqual: true}) - Fuzzy Search:
where.Set("field", value, &gormx.SetWhereOptions{IsFuzzy: true}) - IN:
where.Set("field", values, &gormx.SetWhereOptions{IsIn: true}) - NOT IN:
where.Set("field", values, &gormx.SetWhereOptions{IsNotIn: true})
The GroupBy function returns GroupByResult structs with the following fields:
type GroupByResult struct {
Group map[string]interface{} // Group by field values
Count int64 // Count of records in group
Sum float64 // Sum of numeric field
Avg float64 // Average of numeric field
Min interface{} // Minimum value
Max interface{} // Maximum value
}When using the Aggregate function, you can specify these operations:
"sum"- Sum of values"avg"- Average of values"min"- Minimum value"max"- Maximum value"count"- Count of records"count_distinct"- Count of distinct values
-
Indexes: Ensure your database has appropriate indexes on fields used in WHERE conditions and GROUP BY clauses.
-
Large Datasets: For large datasets, consider using pagination or limiting the scope of your queries.
-
Multiple Aggregations: The
Aggregatefunction is more efficient than running multiple separate aggregate queries.
All aggregate functions return errors that should be handled:
total, err := gormx.Sum[Product]("price", nil)
if err != nil {
log.Printf("Sum query failed: %v", err)
return
}See the examples/aggregate_example.go file for complete working examples of all aggregate functions.