Skip to content

How to get max value by group? #2876

Closed Answered by simolus3
BananaMasterz asked this question in Q&A
Discussion options

You must be logged in to vote

As you may have guessed, this is what GROUP BY can do. So the query would look like this:

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)!;
}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@BananaMasterz
Comment options

Answer selected by BananaMasterz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants