Group and select rows which have min/max value
I wonder if PRQL can solve this very practical problem that many people suffer from every day: https://stackoverflow.com/q/3800551
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.
take 1 should be outside of the [] correct ? Everything inside [ ... ] is a calculation for the aggregation ?
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.
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
sumof 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:
- Different functions — have a different syntax for these cases — e.g.
aggregate_multi_column. - 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 asamount | sum.) 2c. ? - 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!
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.