def distinct(expr: _ColumnExpressionArgument[_T]) -> UnaryExpression[_T]:
"""Produce an column-expression-level unary ``DISTINCT`` clause.
- This applies the ``DISTINCT`` keyword to an individual column
- expression, and is typically contained within an aggregate function,
- as in::
+ This applies the ``DISTINCT`` keyword to an **individual column
+ expression** (e.g. not the whole statement), and renders **specifically
+ in that column position**; this is used for containment within
+ an aggregate function, as in::
from sqlalchemy import distinct, func
- stmt = select(func.count(distinct(users_table.c.name)))
+ stmt = select(users_table.c.id, func.count(distinct(users_table.c.name)))
+
+ The above would produce an statement resembling::
- The above would produce an expression resembling::
+ SELECT user.id, count(DISTINCT user.name) FROM user
- SELECT COUNT(DISTINCT name) FROM user
+ .. tip:: The :func:`_sql.distinct` function does **not** apply DISTINCT
+ to the full SELECT statement, instead applying a DISTINCT modifier
+ to **individual column expressions**. For general ``SELECT DISTINCT``
+ support, use the
+ :meth:`_sql.Select.distinct` method on :class:`_sql.Select`.
The :func:`.distinct` function is also available as a column-level
method, e.g. :meth:`_expression.ColumnElement.distinct`, as in::
:data:`.func`
- """
+ """ # noqa: E501
return UnaryExpression._create_distinct(expr)
@_generative
def distinct(self, *expr: _ColumnExpressionArgument[Any]) -> Self:
r"""Return a new :func:`_expression.select` construct which
- will apply DISTINCT to its columns clause.
+ will apply DISTINCT to the SELECT statement overall.
+
+ E.g.::
+
+ from sqlalchemy import select
+ stmt = select(users_table.c.id, users_table.c.name).distinct()
+
+ The above would produce an statement resembling::
+
+ SELECT DISTINCT user.id, user.name FROM user
+
+ The method also accepts an ``*expr`` parameter which produces the
+ PostgreSQL dialect-specific ``DISTINCT ON`` expression. Using this
+ parameter on other backends which don't support this syntax will
+ raise an error.
:param \*expr: optional column expressions. When present,
- the PostgreSQL dialect will render a ``DISTINCT ON (<expressions>>)``
- construct.
+ the PostgreSQL dialect will render a ``DISTINCT ON (<expressions>)``
+ construct. A deprecation warning and/or :class:`_exc.CompileError`
+ will be raised on other backends.
.. deprecated:: 1.4 Using \*expr in other dialects is deprecated
and will raise :class:`_exc.CompileError` in a future version.