Skip to content

Order by aggregate (like count) in groupBy #5464

@matthewmueller

Description

@matthewmueller

Problem

Right now groupBy doesn't support ordering by an aggregate.

model User {
  id     Int   @id @default(autoincrement())
  town   String
}

List of towns, ordered by number of users in each town.

select town, count(town) 
from user
group by town
order by count(town) asc

Suggested solution

client.user.groupBy({
  orderBy: {
    _count: {
      town: 'desc'
    }
  }
})

Additional context

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions