Aggregations

There are 2 types of aggregation functions: ones which are working without a GROUP clause and returns single values (e.g. max, min, count) and ones, working with GROUP clause and returning arrays of values.

Max

Contact.all.max(:name, String)

Min

Contact.all.min(:age, Int32)

Avg

Contact.all.avg(:age, Float64) # mysql specific
Contact.all.avg(:age, PG::Numeric) # Postgres specific

Sum

Contact.all.sum(:age, Float64) # mysql specific
Contact.all.sum(:age, Int64) # postgres specific

Count

Contact.all.count

Group Max

Contact.all.group(:gender).group_max(:age, Int32)

Group Min

Contact.all.group(:gender).group_min(:age, Int32)

Group Avg

Contact.all.group(:gender).group_avg(:age, Float64) # mysql specific
Contact.all.group(:gender).group_avg(:age, PG::Numeric) # Postgres specific

Group Sum

Contact.all.group(:gender).group_sum(:age, Float64) # mysql specific
Contact.all.group(:gender).group_sum(:age, Int64) # postgres specific

Group Count

Contact.all.group(:gender).group_count(:age)