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.
No description
2 Replies
jsingleton37
jsingleton37OP4mo ago
Oh nice! So I can just do
export async function getYearsThatHaveVotes({ division }: { division: string }) {
const distinctYearsWithVotes = await db
.selectDistinct({
year: weeklyFinalRankings.year,
})
.from(weeklyFinalRankings)

return distinctYearsWithVotes
}
export async function getYearsThatHaveVotes({ division }: { division: string }) {
const distinctYearsWithVotes = await db
.selectDistinct({
year: weeklyFinalRankings.year,
})
.from(weeklyFinalRankings)

return distinctYearsWithVotes
}
Want results from more Discord servers?
Add your server