You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
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 as amount | 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!
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
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
Activity
max-sixty commentedon Jan 25, 2022
I have had this issue too! It's a great case.
How about:
This would involve tweaking the syntax inside the
aggregateexpression though, since in the existing examples it takes a simplesum salaryrather 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.
qorrect commentedon Jan 25, 2022
take 1 should be outside of the [] correct ? Everything inside [ ... ] is a calculation for the aggregation ?
drittich commentedon Jan 25, 2022
Well, you need a way to say "Take 1 from each aggregate". Putting it outside would imply just returning one row.
max-sixty commentedon Feb 13, 2022
Coming back to this as we flesh out the less obvious parts of the language.
There are two goals that this brings up:
sumof a single column & count(*) :The first could be written within the framework of the second, in a verbose way:
...but obviously we don't want to have to do that. So we need some way to do both.
A couple of options:
aggregate_multi_column.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. ?
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
aggregatefunction).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!
[-]Can this select rows corresponding to min/max aggregate group by? [/-][+]Select rows which have min/max value within aggregation[/+][-]Select rows which have min/max value within aggregation[/-][+]Aggregate and select rows which have min/max value[/+]aljazerzen commentedon Apr 11, 2022
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:
This is consistent with the fact that take does not change number or names of the columns (while aggregate does).
It also uses
byparameter 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.
[-]Aggregate and select rows which have min/max value[/-][+]Group and select rows which have min/max value[/+]