Aggregate
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).
Parameters
Section titled “Parameters”
(opRelationship) - The aggregate operator (typically created by the standard aggregate helpers).
(*argsValue|Distinct, default:()) - Values to aggregate. Pass a singleDistinctto aggregate over unique values.
(distinctbool, default:False) - If True, treat the aggregate as distinct.
Most users should not instantiate this class directly.
Methods
Section titled “Methods”.where()
Section titled “.where()”Aggregate.where(*args: Statement) -> AggregateReturn 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:
(*argsStatement, default:()) - One or moresemantics.frontend.base.Statementitems to use as filter conditions.
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).per()
Section titled “.per()”Aggregate.per(*args: Value) -> AggregateReturn 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:
(*argsValue, 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)Inheritance Hierarchy
Section titled “Inheritance Hierarchy”Used By
Section titled “Used By”semantics > frontend > base └── Statement
Returned By
Section titled “Returned By”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
Referenced By
Section titled “Referenced By”RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works > Use advanced reasoning > Rules-based reasoning └── Aggregate and group data ├── Group results withper()└── Scope aggregate inputs with aggregate-local.where()