MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / within_group

Function within_group

lib/sqlalchemy/sql/_elements_constructors.py:1846–1884  ·  view source on GitHub ↗

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. :func:`_expression.within_group` is usually call

(
    element: FunctionElement[_T], *order_by: _ColumnExpressionArgument[Any]
)

Source from the content-addressed store, hash-verified

1844
1845
1846def within_group(
1847 element: FunctionElement[_T], *order_by: _ColumnExpressionArgument[Any]
1848) -> WithinGroup[_T]:
1849 r"""Produce a :class:`.WithinGroup` object against a function.
1850
1851 Used against so-called "ordered set aggregate" and "hypothetical
1852 set aggregate" functions, including :class:`.percentile_cont`,
1853 :class:`.rank`, :class:`.dense_rank`, etc.
1854
1855 :func:`_expression.within_group` is usually called using
1856 the :meth:`.FunctionElement.within_group` method, e.g.::
1857
1858 from sqlalchemy import within_group
1859
1860 stmt = select(
1861 department.c.id,
1862 func.percentile_cont(0.5).within_group(department.c.salary.desc()),
1863 )
1864
1865 The above statement would produce SQL similar to
1866 ``SELECT department.id, percentile_cont(0.5)
1867 WITHIN GROUP (ORDER BY department.salary DESC)``.
1868
1869 :param element: a :class:`.FunctionElement` construct, typically
1870 generated by :data:`~.expression.func`.
1871 :param \*order_by: one or more column elements that will be used
1872 as the ORDER BY clause of the WITHIN GROUP construct.
1873
1874 .. seealso::
1875
1876 :ref:`tutorial_functions_within_group` - in the
1877 :ref:`unified_tutorial`
1878
1879 :data:`.expression.func`
1880
1881 :func:`_expression.over`
1882
1883 """
1884 return WithinGroup(element, *order_by)

Callers 2

test_within_groupMethod · 0.90

Calls 1

WithinGroupClass · 0.85

Tested by 2

test_within_groupMethod · 0.72