sum

abstract fun <T : Any> sum(property: String, type: KClass<T>): RealmScalarQuery<T>

Calculates the sum of the given property.

If the aggregated result of the property does not fit into the specified type the result will overflow following Kotlin's semantics for said type. For example, if the property floor is a Byte and the specified type is also Byte, e.g. query.sum("floor", Short::class), the result will overflow for values greater than Byte.MAX_VALUE. It is possible to circumvent this limitation by specifying a type which is less likely to overflow in the query, e.g. query.sum("floor", Int::class). In this case the aggregated value will be an Int.

A reified version of this method is also available as an extension function, query.sum<YourClass>(...). Import io.realm.query.sum to access it.

Return

the sum of fields of the matching objects. If no objects exist or they all have null as the value for the given property, 0 will be returned. When computing the sum, objects with null values are ignored.

Parameters

property

the property to sum. Only Number properties are supported.

type

the type of the resulting aggregated value, which may or may not coincide with the type of the property itself.

Throws

if the property is not a Number or a Char, or if it is a RealmInstant, or if the type cannot be used to represent the property.