sum()#

relationalai.std.aggregates
#sum(*args: Producer, per: list[Producer] = []) -> Expression

Calculates the sum of all distinct values produced by one or more Producer objects. When multiple producers are provided, the aggregation is done over rows (v1, v2, ... vn) representing the distinct combinations of values produced by the arguments, with the sum taken over the last column. Pass a list of Producer objects to the optional per parameter to group values and compute the sum per group. Must be called in a rule or query context.

Parameters#

NameTypeDescription
*argsProducerOne or more Producer objects.
perList[Producer]An optional list of Producer objects used to group arguments. (Default: [])

Returns#

An Expression object.

Example#

Use sum() to compute the sum of distinct values produced by a Producer object:

#import relationalai as rai
from relationalai.std import aggregates, alias


# =====
# SETUP
# =====

model = rai.Model("MyModel")
Person = model.Type("Person")

with model.rule():
    Person.add(id=1).set(name="Joe", age=20)
    Person.add(id=2).set(name="Jane", age=20)
    Person.add(id=3).set(name="John", age=30)


# =======
# EXAMPLE
# =======

with model.query() as select:
    person = Person()
    # Compute the sum of the person.age property. Note that properties use set
    # semantics, so the sum is computed over distinct age values and not over
    # the ages of each person.
    total_age = aggregates.sum(person.age)
    response = select(alias(total_age, "total_age_distinct"))

print(response.results)
#    total_age_distinct
# 0                  50

When you pass multiple Producer objects to sum(), the aggregation is performed over the expansion of the arguments into distinct rows of the form (v1, v2, ..., vn) where v1, v2, …, vn are the values produced by the Producer objects. The sum is taken over the values in the last column of each row.

You can use this, for example, to compute the sum of ages of each person in the preceding example:

## Get the sum of ages for all people.
with model.query() as select:
    person = Person()
    total_age = aggregates.sum(person, person.age)
    response = select(alias(total_age, "total_age"))

print(response.results)
#    total_age
# 0         70

To group values and compute the sum for each group, pass a list of one or more Producer objects to the optional per keyword argument. In the following example, the person object is passed to per to compute the sum of a person’s friends’ ages:

## Set a multi-valued friend property for each person.
with model.rule():
    joe = Person(name="Joe")
    jane = Person(name="Jane")
    john = Person(name="John")
    joe.friends.extend([jane, john])
    jane.friends.extend([joe])
    john.friends.extend([joe])

with model.query() as select:
    person = Person()
    # Compute the sum of ages of a person's friends.
    sum_friend_age = aggregates.sum(person.friends, person.friends.age, per=[person])
    response = select(person.name, alias(sum_friend_age, "sum_friend_age"))

print(response.results)
#    name  sum_friend_age
# 0  Jane              20
# 1   Joe              50
# 2  John              20

See Also#