Skip to content

Aggregate

relationalai.semantics.frontend.base
Aggregate(op: Relationship, *args: Value | Distinct, distinct: bool = False)

Represents an aggregate computation in the DSL.

Aggregate objects are returned by aggregate helpers such as count and sum. Use Aggregate.per to add grouping keys (like SQL GROUP BY) and Aggregate.where to restrict which bindings contribute to the aggregate.

To aggregate over unique values, wrap arguments with Model.distinct (or distinct).

  • op

    (Relationship) - The aggregate operator (typically created by the standard aggregate helpers).
  • *args

    (Value | Distinct, default: ()) - Values to aggregate. Pass a single Distinct to aggregate over unique values.
  • distinct

    (bool, default: False) - If True, treat the aggregate as distinct.

Most users should not instantiate this class directly.

Aggregate.where(*args: Statement) -> Aggregate

Return a new aggregate restricted to bindings that satisfy the given conditions.

This behaves like filtering inside the aggregate: only matches that satisfy the conditions contribute to the aggregate result. (It does not filter the rows of the surrounding query; use Fragment.where for that.)

Parameters:

Returns:

  • Aggregate - A new aggregate with the additional filter(s) applied.

Examples:

Count only employees older than 30 per department:

>>> from relationalai.semantics import Integer, Model, count
>>> m = Model()
>>> Department = m.Concept("Department")
>>> Employee = m.Concept("Employee")
>>> Employee.age = m.Property(f"{Employee} is {Integer:age} years old")
>>> Department.employees = m.Relationship(f"{Department} has {Employee:employees}")
>>> employees = Department.employees
>>> count(employees).per(Department).where(employees.age > 30)
Aggregate.per(*args: Value) -> Aggregate

Return a new aggregate grouped by the given key(s).

This behaves like adding SQL GROUP BY keys: the aggregate is computed separately for each unique combination of the grouping values.

Parameters:

  • *args

    (Value, default: ()) - One or more values to group by.

Returns:

  • Aggregate - A new aggregate with the additional grouping key(s) applied.

Examples:

Count employees per department:

>>> from relationalai.semantics import Model, count
>>> m = Model()
>>> Department = m.Concept("Department")
>>> Employee = m.Concept("Employee")
>>> Department.employees = m.Relationship(f"{Department} has {Employee:employees}")
>>> count(Department.employees).per(Department)
AggregateVariableDSLBase
 semantics
├──  frontend > base
│   └──  Aggregate
│       ├──  per
│       └──  where
└──  std > aggregates
    ├──  Per
    │   ├──  avg
    │   ├──  bottom
    │   ├──  count
    │   ├──  limit
    │   ├──  max
    │   ├──  min
    │   ├──  rank
    │   ├──  rank_asc
    │   ├──  rank_desc
    │   ├──  string_join
    │   ├──  sum
    │   └──  top
    ├──  avg
    ├──  bottom
    ├──  count
    ├──  limit
    ├──  max
    ├──  min
    ├──  rank
    ├──  rank_asc
    ├──  rank_desc
    ├──  string_join
    ├──  sum
    └──  top
RelationalAI Documentation
└──  Build With RelationalAI
    └──  Understand how PyRel works > Use advanced reasoning > Rules-based reasoning
        └──  Aggregate and group data
            ├──  Group results with per()
            └──  Scope aggregate inputs with aggregate-local .where()