r"""Produce a :class:`.WithinGroup` object against a function. Used against so-called "ordered set aggregate" and "hypothetical set aggregate" functions, including :class:`.percentile_cont`, :class:`.rank`, :class:`.dense_rank`, etc. This feature is typically used by Oracle Databas
(
element: FunctionElement[_T], *order_by: _ColumnExpressionArgument[Any]
)
| 2067 | |
| 2068 | |
| 2069 | def within_group( |
| 2070 | element: FunctionElement[_T], *order_by: _ColumnExpressionArgument[Any] |
| 2071 | ) -> WithinGroup[_T]: |
| 2072 | r"""Produce a :class:`.WithinGroup` object against a function. |
| 2073 | |
| 2074 | Used against so-called "ordered set aggregate" and "hypothetical |
| 2075 | set aggregate" functions, including :class:`.percentile_cont`, |
| 2076 | :class:`.rank`, :class:`.dense_rank`, etc. This feature is typically |
| 2077 | used by Oracle Database, Microsoft SQL Server. |
| 2078 | |
| 2079 | For generalized ORDER BY of aggregate functions on all included |
| 2080 | backends, including PostgreSQL, MySQL/MariaDB, SQLite as well as Oracle |
| 2081 | and SQL Server, the :func:`_sql.aggregate_order_by` provides a more |
| 2082 | general approach that compiles to "WITHIN GROUP" only on those backends |
| 2083 | which require it. |
| 2084 | |
| 2085 | :func:`_expression.within_group` is usually called using |
| 2086 | the :meth:`.FunctionElement.within_group` method, e.g.:: |
| 2087 | |
| 2088 | stmt = select( |
| 2089 | func.percentile_cont(0.5).within_group(department.c.salary.desc()), |
| 2090 | ) |
| 2091 | |
| 2092 | The above statement would produce SQL similar to |
| 2093 | ``SELECT percentile_cont(0.5) |
| 2094 | WITHIN GROUP (ORDER BY department.salary DESC)``. |
| 2095 | |
| 2096 | :param element: a :class:`.FunctionElement` construct, typically |
| 2097 | generated by :data:`~.expression.func`. |
| 2098 | :param \*order_by: one or more column elements that will be used |
| 2099 | as the ORDER BY clause of the WITHIN GROUP construct. |
| 2100 | |
| 2101 | .. seealso:: |
| 2102 | |
| 2103 | :ref:`tutorial_functions_within_group` - in the |
| 2104 | :ref:`unified_tutorial` |
| 2105 | |
| 2106 | :func:`_sql.aggregate_order_by` - helper for PostgreSQL, MySQL, |
| 2107 | SQLite aggregate functions |
| 2108 | |
| 2109 | :data:`.expression.func` |
| 2110 | |
| 2111 | :func:`_expression.over` |
| 2112 | |
| 2113 | """ |
| 2114 | return WithinGroup(element, *order_by) |
| 2115 | |
| 2116 | |
| 2117 | def aggregate_order_by( |