Skip to content

Support max_by in Aggregation function #12252

Description

@Weijun-H

Is your feature request related to a problem or challenge?

max_by(arg, val)

Description Finds the row with the maximum val. Calculates the arg expression at that row. This function is affected by ordering.
Example max_by(A, B)
Alias(es) argMax(arg, val), arg_max(arg, val)

Describe the solution you'd like

D SELECT max_by(x, y) FROM VALUES ('a', 10), ('b', 50), ('c', 20) AS tab(x, y);
┌──────────────┐
│ max_by(x, y) │
│   varchar    │
├──────────────┤
│ b            │
└──────────────┘

Describe alternatives you've considered

No response

Additional context

https://duckdb.org/docs/sql/functions/aggregates#max_byarg-val
https://docs.databricks.com/en/sql/language-manual/functions/max_by.html

Activity

  1. Lordworms commented on Aug 30, 2024

    @Lordworms
    Contributor

    take

  2. korowa commented on Sep 1, 2024

    @korowa
    Contributor

    These probably could be an aliases (with some additional transformations) for first/last aggregation functions -- otherwise there it'll end with two implementations of basically the same function (not something bad, but still redundant).

    Relevant discussion -- #12075

  3. alamb commented on Sep 9, 2024

    @alamb
    Contributor

    Given #12357 we may want to move this function to some other repo rather than the core

  4. alamb commented on Sep 16, 2024

    @alamb
    Contributor

    Suggestion: #12254 (comment)

  5. alamb commented on Sep 25, 2024

    @alamb
    Contributor

    We are going to implement these in a different repository so closing this ticket to avoid confusion: #12254 (comment)

  6. findepi commented on Jul 16, 2025

    @findepi
    Member

    I'd love to see min_by & max_by within DataFusion repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions