snowflake.snowpark.functions.max_by

snowflake.snowpark.functions.max_by(col_to_return: Union[Column, str], col_containing_maximum: Union[Column, str], maximum_number_of_values_to_return: Optional[int] = None) Column[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.29.1/snowpark-python/src/snowflake/snowpark/functions.py#L11049-L11080)

Finds the row(s) containing the maximum value for a column and returns the value of another column in that row.

Example:

>>> df = session.create_dataframe([
...     [1001, 10, 10000],
...     [1020, 10, 9000],
...     [1030, 10, 8000],
...     [900, 20, 15000],
...     [2000, 20, None],
...     [2010, 20, 15000],
...     [2020, 20, 8000]
... ], schema=["employee_id", "department_id", "salary"])
>>> df.select(max_by("employee_id", "salary", 3)).collect()
[Row(MAX_BY("EMPLOYEE_ID", "SALARY", 3)='[\n  900,\n  2010,\n  1001\n]')]
Copy
Language: English