Skip to content

Group and select rows which have min/max value #9

Closed

Description

@vmarkovtsev

I wonder if PRQL can solve this very practical problem that many people suffer from every day: https://stackoverflow.com/q/3800551

Activity

  1. max-sixty commented on Jan 25, 2022

    @max-sixty
    Member

    I have had this issue too! It's a great case.

    How about:

    from purchases
    aggregate by:customer_id [
      sort order:desc total
      select id
      take 1
    ]

    This would involve tweaking the syntax inside the aggregate expression though, since in the existing examples it takes a simple sum salary rather than a full pipeline over the group, which this requires.

    And then we'd need to figure out which target to transpile to and how to do that — while I'm holding that we can make lots of progress over SQL without doing anything too impressive there — this sort of problem may challenge that assumption.

    As ever open to feedback, thanks for the case.

  2. qorrect commented on Jan 25, 2022

    @qorrect
    Contributor

    take 1 should be outside of the [] correct ? Everything inside [ ... ] is a calculation for the aggregation ?

  3. drittich commented on Jan 25, 2022

    @drittich

    take 1 should be outside of the [] correct ? Everything inside [ ... ] is a calculation for the aggregation ?

    Well, you need a way to say "Take 1 from each aggregate". Putting it outside would imply just returning one row.

  4. max-sixty commented on Feb 13, 2022

    @max-sixty
    Member

    Coming back to this as we flesh out the less obvious parts of the language.

    There are two goals that this brings up:

    • Column-specific aggregations — very simple aggregations, like the sum of a single column & count(*) :
      from purchases
      aggregate by:customer_id [
        sum amount,    # note this is the same as `amount | sum`
        count,
      ]
    • Multi-column aggregations — more complex expressions which do some transformation involving multiple columns (even if they only return one column). Example from above:
      from purchases
      aggregate by:customer_id [
        (
          sort order:desc amount
          select id    # Possibly we could also allow returning multiple columns; like `select [id, amount]`
          take 1
        ),             # The `id` with the largest amount
        (
          sort order:asc amount
          select id
          take 1
        ),             # The `id` with the smallest amount
      ]

    The first could be written within the framework of the second, in a verbose way:

    from purchases
    
    aggregate by:customer_id [
      (
        select amount
        sum
      ),
      (
        count
      ),
    ]

    ...but obviously we don't want to have to do that. So we need some way to do both.

    A couple of options:

    1. Different functions — have a different syntax for these cases — e.g. aggregate_multi_column.
    2. Inspect the expression:
      2a. The multi-column aggregations contain select (But would they always? What if they can return > 1 column?)
      2b. The single-column expressions "start" with a single column name ("start" if we consider it as amount | sum.)
      2c. ?
    3. Open to other ideas...

    I'd really prefer not to have multiple functions. But if we inspect the expression we need to have a rule that's obvious.

    We can also consider how window / analytical functions will work — these are another sort of more complex transformation (though they don't aggregate, so wouldn't be in an aggregate function).

    One final note — while it'd be simpler if all of these quandries weren't quandries — it's worth considering how difficult this is in SQL. The SO link at the top has 1721 upvotes, multiple somewhat conflicting 1000+ upvoted answers, and no consensus on the best approach!

  5. added
    language-design
    on Feb 13, 2022
  6. changed the title [-]Can this select rows corresponding to min/max aggregate group by? [/-] [+]Select rows which have min/max value within aggregation[/+] on Apr 1, 2022
  7. changed the title [-]Select rows which have min/max value within aggregation[/-] [+]Aggregate and select rows which have min/max value[/+] on Apr 1, 2022
  8. aljazerzen commented on Apr 11, 2022

    @aljazerzen
    Member

    Again, im against having pipelines in transform arguments. I think that take and sort should not be within aggregate argument.

    It would be most intuitive to be able to write:

    from purchases
    sort order:desc total
    take 1 by:customer_id
    

    This is consistent with the fact that take does not change number or names of the columns (while aggregate does).

    It also uses by parameter as indication that we are doing partitioning.

    But another question is how would we trnaslate this to SQL. As said SO gives a few answers, depending on SQL dialect and database version.

  9. changed the title [-]Aggregate and select rows which have min/max value[/-] [+]Group and select rows which have min/max value[/+] on May 16, 2022
  10. linked a pull request that will close this issueGroup and take a range #564on May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    language-design

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions