How to get max value by group? #2876
-
I have a table that is like:
How can I get the max score of each person? So the result would be:
|
Beta Was this translation helpful? Give feedback.
Answered by
simolus3
Feb 2, 2024
Replies: 1 comment 1 reply
-
As you may have guessed, this is what final maxScore = table.score.max();
final query = selectOnly(table)
..addColumns([table.name, maxScore])
..groupBy([table.name]);
for (final row in await query.get()) {
final name = row.read(table.name)!;
final score = row.read(maxScore)!;
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
BananaMasterz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As you may have guessed, this is what
GROUP BY
can do. So the query would look like this: