Produce an column-expression-level unary ``DISTINCT`` clause. 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,
(expr: _ColumnExpressionArgument[_T])
| 1170 | |
| 1171 | |
| 1172 | def distinct(expr: _ColumnExpressionArgument[_T]) -> UnaryExpression[_T]: |
| 1173 | """Produce an column-expression-level unary ``DISTINCT`` clause. |
| 1174 | |
| 1175 | This applies the ``DISTINCT`` keyword to an **individual column |
| 1176 | expression** (e.g. not the whole statement), and renders **specifically |
| 1177 | in that column position**; this is used for containment within |
| 1178 | an aggregate function, as in:: |
| 1179 | |
| 1180 | from sqlalchemy import distinct, func |
| 1181 | |
| 1182 | stmt = select(users_table.c.id, func.count(distinct(users_table.c.name))) |
| 1183 | |
| 1184 | The above would produce an statement resembling: |
| 1185 | |
| 1186 | .. sourcecode:: sql |
| 1187 | |
| 1188 | SELECT user.id, count(DISTINCT user.name) FROM user |
| 1189 | |
| 1190 | .. tip:: The :func:`_sql.distinct` function does **not** apply DISTINCT |
| 1191 | to the full SELECT statement, instead applying a DISTINCT modifier |
| 1192 | to **individual column expressions**. For general ``SELECT DISTINCT`` |
| 1193 | support, use the |
| 1194 | :meth:`_sql.Select.distinct` method on :class:`_sql.Select`. |
| 1195 | |
| 1196 | The :func:`.distinct` function is also available as a column-level |
| 1197 | method, e.g. :meth:`_expression.ColumnElement.distinct`, as in:: |
| 1198 | |
| 1199 | stmt = select(func.count(users_table.c.name.distinct())) |
| 1200 | |
| 1201 | The :func:`.distinct` operator is different from the |
| 1202 | :meth:`_expression.Select.distinct` method of |
| 1203 | :class:`_expression.Select`, |
| 1204 | which produces a ``SELECT`` statement |
| 1205 | with ``DISTINCT`` applied to the result set as a whole, |
| 1206 | e.g. a ``SELECT DISTINCT`` expression. See that method for further |
| 1207 | information. |
| 1208 | |
| 1209 | .. seealso:: |
| 1210 | |
| 1211 | :meth:`_expression.ColumnElement.distinct` |
| 1212 | |
| 1213 | :meth:`_expression.Select.distinct` |
| 1214 | |
| 1215 | :data:`.func` |
| 1216 | |
| 1217 | """ # noqa: E501 |
| 1218 | if isinstance(expr, operators.ColumnOperators): |
| 1219 | return expr.distinct() |
| 1220 | else: |
| 1221 | return UnaryExpression._create_distinct(expr) |
| 1222 | |
| 1223 | |
| 1224 | def bitwise_not(expr: _ColumnExpressionArgument[_T]) -> UnaryExpression[_T]: |