module Jennifer::QueryBuilder::Aggregations

Overview

Contains aggregation query functions.

Direct including types

Defined in:

jennifer/query_builder/aggregations.cr

Instance Method Summary

Instance Method Detail

def avg(field, klass : T.class) : T forall T #

Returns average value of the field field of type klass.

field is pasted as is into the query. Also ArgumentError is raised if any grouping is already specified.

# for MySQL
Jennifer::Query["contacts"].avg("age", Float64) # => 17.5
# for PostgreSQL
Jennifer::Query["contacts"].avg("age", PG::Numeric) # => 17.5 of PG::Numeric

[View source]
def count : Int32 #

Returns result row count.

Jennifer::Query["contacts"].count # => 123

[View source]
def group_avg(field, klass : T.class) : Array(T) forall T #

Returns array of average values of the field field of type klass in groups.

field is pasted as is into the query.

# MySQL
Jennifer::Query["contacts"].group(:city_id).group_avg("age", Float64) # => [45.0, 39.0]
# PostgreSQL
Jennifer::Query["contacts"].group(:city_id).group_avg("age", PG::Numeric) # => [45.0, 39.0] of PG::Numeric

[View source]
def group_count(field) #

Returns array of counts values of the field field of type klass in groups.

field is pasted as is into the query.

Jennifer::Query["contacts"].group(:city_id).group_count("age", Int32) # => [45, 39]

[View source]
def group_max(field, klass : T.class) : Array(T) forall T #

Returns array of maximum values of the field field of type klass in groups.

field is pasted as is into the query.

Jennifer::Query["contacts"].group(:city_id).group_max("age", Int32) # => [45, 39]

[View source]
def group_min(field, klass : T.class) : Array(T) forall T #

Returns array of minimum values of the field field of type klass in groups.

field is pasted as is into the query.

Jennifer::Query["contacts"].group(:city_id).group_min("age", Int32) # => [45, 39]

[View source]
def group_sum(field, klass : T.class) : Array(T) forall T #

Returns array of values sums of the field field of type klass in groups.

field is pasted as is into the query.

# MySQL
Jennifer::Query["contacts"].group(:city_id).group_sum("age", Float64) # => [45.0, 39.0]
# PostgreSQL
Jennifer::Query["contacts"].group(:city_id).group_sum("age", Int64) # => [45, 39] of Int64

[View source]
def max(field, klass : T.class) : T forall T #

Returns maximum value of the field field of type klass.

field is pasted as is into the query. Also ArgumentError is raised if any grouping is already specified.

Jennifer::Query["contacts"].max("age", Int32) # => 45

[View source]
def min(field, klass : T.class) : T forall T #

Returns minimum value of the field field of type klass.

field is pasted as is into the query. Also ArgumentError is raised if any grouping is already specified.

Jennifer::Query["contacts"].min("age", Int32) # => 18

[View source]
def sum(field, klass : T.class) : T forall T #

Returns sum of the field field of type klass.

field is pasted as is into the query. Also ArgumentError is raised if any grouping is already specified.

# for MySQL
Jennifer::Query["contacts"].sum("age", Float64) # => 1000.0
# for PostgreSQL
Jennifer::Query["contacts"].sum("age", Int64) # => 1000i64

[View source]