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.

Support aggregations (e.g. count) on nested relations

See original GitHub issue

Problem

Right now if you want to just get the count of the model in the same query, it is not possible.

Suggested solution

Introduce an aggregate key in include/select for nested relation in addition to the where key

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

Alternatives

Doing a separate aggregate call for fetching the count.

Additional context

Suggestion by @kitze from the livestreams.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:180
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
nrxuscommented, Jan 20, 2021

Does anyone have an alternative for getting the “user” with the highest number of “posts”?

I have been trying to wrap my head around the current aggregate preview api, but it doesn’t seem possible to get the record with the highest count of related records. I am guessing it is not currently possible in prisma (in a single prisma statement)?

9reactions
rodrigoehlerscommented, Jan 7, 2021

This would be very useful. Any update on this? Is this being worked on or planned to be worked on?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Aggregation, grouping, and summarizing (Concepts) - Prisma
Prisma Client allows you to count records, aggregate number fields, and select distinct field values. Aggregate. Prisma Client allows you to aggregate on ......
Read more >
Nested aggregation | Elasticsearch Guide [8.5] | Elastic
A special single bucket aggregation that enables aggregating nested documents. For example, lets say we have an index of products, and each product...
Read more >
How can I get ElasticSearch aggregations to count the parent ...
Use reverse nested aggregation. This will then create an aggregation with the nested counts and a sub aggregation with the parent counts.
Read more >
Aggregations in Power Pivot - Microsoft Support
The decision of how to group the data is driven by the business question. For example, aggregations can answer the following questions: Counts...
Read more >
Practical planning and execution of groupjoin and nested ...
Here, nested the query calculates a COUNT(*) over the inner table, ... empty to support the semantics of static (whole table) aggregation.
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