prql icon indicating copy to clipboard operation
prql copied to clipboard

Group and select rows which have min/max value

Open vmarkovtsev opened this issue 4 years ago • 5 comments

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

vmarkovtsev avatar Jan 25 '22 06:01 vmarkovtsev

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.

max-sixty avatar Jan 25 '22 07:01 max-sixty

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

qorrect avatar Jan 25 '22 13:01 qorrect

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.

drittich avatar Jan 25 '22 17:01 drittich

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!

max-sixty avatar Feb 13 '22 06:02 max-sixty

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.

aljazerzen avatar Apr 11 '22 13:04 aljazerzen