© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
2 replies
jsingleton37

What is the best way to get distinct values from a table?

I am curious what would be the best way to get distinct values from a table. I have

export async function getYearsThatHaveVotes({ division }: { division: string }) {
  const yearsWithVotes = await db
    .select({
      year: sql<number>`DISTINCT ${weeklyFinalRankings.year}`,
    })
    .from(weeklyFinalRankings)
    .where(sql`${weeklyFinalRankings.division} = ${division}`)
    .orderBy(sql`${weeklyFinalRankings.year} DESC`)

  return yearsWithVotes
}
export async function getYearsThatHaveVotes({ division }: { division: string }) {
  const yearsWithVotes = await db
    .select({
      year: sql<number>`DISTINCT ${weeklyFinalRankings.year}`,
    })
    .from(weeklyFinalRankings)
    .where(sql`${weeklyFinalRankings.division} = ${division}`)
    .orderBy(sql`${weeklyFinalRankings.year} DESC`)

  return yearsWithVotes
}


But it just feels weird importing the schema to use it.
image.png
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Best way to select 10 random rows from a table?
Drizzle TeamDTDrizzle Team / help
2y ago
What is the best way to handle Query Errors?
Drizzle TeamDTDrizzle Team / help
9mo ago
What is the best way to create conditional filters on a query?
Drizzle TeamDTDrizzle Team / help
3y ago
What is the best way to order by exact matches
Drizzle TeamDTDrizzle Team / help
2y ago