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.

`count()` support distinct

See original GitHub issue

Problem

I have bumped into a few cases where I need to do a ‘where’ filter and get records that are ‘distinct’, to add pagination I need to do a count query also with a ‘distinct’ option on ‘count()’, based on the documentation and experience, I can’t use distinct on a count()

export type FindManyUserArgs = {
  where?: UserWhereInput | null
  orderBy?: Enumerable<UserOrderByInput> | null
  skip?: number | null
  after?: UserWhereUniqueInput | null
  before?: UserWhereUniqueInput | null
  first?: number | null
  last?: number | null
}

Suggested solution

Can we update count() to support distinct as well? so it would accept the object similar to findMany()

export type FindManyUserArgs = {
  select?: UserSelect | null
  include?: UserInclude | null
  where?: UserWhereInput | null
  orderBy?: Enumerable<UserOrderByInput> | null
  cursor?: UserWhereUniqueInput | null
  take?: number | null
  skip?: number | null
  distinct?: Enumerable<UserDistinctFieldEnum>  
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:78
  • Comments:25 (1 by maintainers)

github_iconTop GitHub Comments

16reactions
chinandermcommented, Mar 8, 2021

I would really love to see this feature added.

10reactions
kentcdoddscommented, Aug 20, 2021

For folks who need this, here’s my workaround until it’s supported:

 async function getReaderCount() {
   // couldn't figure out how to do this in one query without $queryRaw 🤷‍♂️
   type CountResult = [{count: number}]
   const [userIdCount, clientIdCount] = await Promise.all([
     prisma.$queryRaw`SELECT COUNT(DISTINCT "public"."PostRead"."userId") FROM "public"."PostRead" WHERE ("public"."PostRead"."userId") IS NOT NULL` as Promise<CountResult>,
     prisma.$queryRaw`SELECT COUNT(DISTINCT "public"."PostRead"."clientId") FROM "public"."PostRead" WHERE ("public"."PostRead"."clientId") IS NOT NULL` as Promise<CountResult>,
   ]).catch(() => [[{count: 0}], [{count: 0}]])
   return userIdCount[0].count + clientIdCount[0].count
 }
Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL COUNT() with DISTINCT - w3resource
SQL COUNT() function with DISTINCT clause eliminates the repetitive appearance of the same data. The DISTINCT can come only once in a given ......
Read more >
Count unique values among duplicates - Microsoft Support
Let's say you want to find out how many unique values exist in a range that contains duplicate values. For example, if a...
Read more >
Overview of the SQL Count Distinct Function - SQLShack
COUNT. Count(*). Count(Distinct) ; It returns the total number of rows after satisfying conditions specified in the where clause. It returns the ...
Read more >
How to Count Distinct Values in SQL - LearnSQL.com
To count the number of different values that are stored in a given column, you simply need to designate the column you pass...
Read more >
Count Distinct and Window Functions - Simple Talk
Count Distinct is not supported by window partitioning, we need to find a different way to achieve the same result.
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