question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Order by aggregate (like count) in groupBy

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:5
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
simontaisnecommented, Feb 23, 2022

This would grab a count of posts on a user, ordered by the users with the most posts, and then take the highest 6

Hmmm never mind, I guess another way to achieve it would be to do the following.

const users = await prisma.user.findMany({
  include: {
    _count: {
      select: { posts: true },
    },
  },
  orderBy: {
    posts: {
      _count: 'desc'
    }
  },
  take: 6
})
4reactions
zackifycommented, Apr 10, 2021

Just want to follow up, with the latest version of prisma, you can almost do this. But not quite:

const users = await prisma.user.findMany({
  include: {
    _count: {
      select: { posts: true },
    },
  },
})

You can do the above, but I need to be able to group by the highest count, and take only a few. Something like:

const users = await prisma.user.findMany({
  include: {
    _count: {
      select: { posts: true },
    },
  },
  orderBy: {
    _count: {
      posts: 'desc'
    }
  },
  take: 6
})

This would grab a count of posts on a user, ordered by the users with the most posts, and then take the highest 6

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pandas groupby() and count() with Examples
groupBy () function is used to collect the identical data into groups and perform aggregate functions like size/count on the grouped data.
Read more >
How to Order by Count in SQL? | LearnSQL.com
The first step is to use the GROUP BY clause to create the groups (in our example, we group by the country column)....
Read more >
Aggregation, grouping, and summarizing (Concepts) - Prisma
Aggregation, grouping, and summarizing. Prisma Client allows you to count records, aggregate number fields, and select distinct field values.
Read more >
Get statistics for each group (such as count, mean, etc) using ...
Quick Answer: The simplest way to get row counts per group is by calling .size() , which returns a Series : df.groupby(['col1','col2']).size ...
Read more >
SQL COUNT() with GROUP by - w3resource
The GROUP BY makes the result set in summary rows by the value of one or more columns. Each same value on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found