Order by aggregate (like count) in groupBy
See original GitHub issueProblem
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
- First mentioned in: https://github.com/prisma/prisma/issues/4893#issuecomment-766146759
- There’s a similar, but distinct feature request for ordering by relation: https://github.com/prisma/prisma/issues/5439
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:9 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hmmm never mind, I guess another way to achieve it would be to do the following.
Just want to follow up, with the latest version of prisma, you can almost do this. But not quite:
You can do the above, but I need to be able to group by the highest count, and take only a few. Something like:
This would grab a count of posts on a user, ordered by the users with the most posts, and then take the highest 6