distinct
relationalai.semantics
distinct(*args: Value) -> DistinctMark values as distinct (unique) for use in select and aggregates.
This is a convenience wrapper around Model.distinct that operates
on the single active model. It returns a Distinct object.
distinct is used in two common ways:
- As a
SELECT DISTINCT-style wrapper by passing it as the only argument toselect, e.g.select(distinct(x, y)). - As an argument wrapper for aggregates to make them count/sum/etc. over
unique values, e.g.
count(distinct(Person.name)).
If you have more than one model, call Model.distinct on the specific model instead.
Parameters
Section titled “Parameters”
(*argsValue, default:()) - One or moresemantics.frontend.base.Valueitems to consider when removing duplicates.
Returns
Section titled “Returns”Raises
Section titled “Raises”relationalai.util.error.RAIException- Raised in the following cases:- There is no active model, or multiple models exist and the active model would be ambiguous.
distinctis used incorrectly (for example, not applied to the entireselect(...), or not applied to all aggregate arguments).
Examples
Section titled “Examples”Count unique names (like SQL COUNT(DISTINCT name)):
>>> from relationalai.semantics import Model, Concept, count, distinct, select>>> m = Model()>>> Person = m.Concept("Person")>>> define(... Person.new(id=1, name="Alice"),... Person.new(id=2, name="Bob"),... Person.new(id=3, name="Alice"),... )>>> select(count(distinct(Person.name))).to_df()Select unique rows by wrapping all selected values:
>>> counts = count(Person).per(Person.name)>>> select(distinct(Person.name, counts)).to_df()Incorrect vs correct usage for distinct rows:
>>> # Incorrect: only one column wrapped; this is *not* a distinct-row select.>>> select(Person.name, distinct(Person.age))>>> # Correct: wrap the full selected row.>>> select(distinct(Person.name, Person.age))Referenced By
Section titled “Referenced By”RelationalAI Documentation └── Build With RelationalAI └── Understand how PyRel works ├── Build a semantic model │ └── Query a model │ └── Deduplicate rows withdistinct└── Use advanced reasoning > Rules-based reasoning └── Aggregate and group data └── Deduplicate aggregate inputs withdistinct()