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
(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.
Notes
Most users should not instantiate this class directly.
Methods
.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.employeescount(employees).per(Department).where(employees.age > 30).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)Notes:
A value-typed key (a String, Date, Boolean or a number) has to be given
values somewhere in its scope. An entity key needs no binding — its
Concept supplies the groups — but a value-typed one has no such list,
so grouping by one that nothing binds is refused: give it values with a
.where(...), or drop it from the grouping.
Referenced By: