Bård
08/17/2021, 10:09 AMmodel Seed {
id String @id @default(cuid())
uid String
score Int?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [uid], references: [id])
tasks Tasks[]
}
I want to query to create a highscore list based on the score value. But I also want to find the users relation to that highscore. Say that the users score is number 400 in the highscore list. Is there any way to query the model and sort by score, then count the amount of entries up to the first entry that contains the user?Ryan
08/18/2021, 7:32 AMawait prisma.seed.findMany({
where: { score: { gte: 400 } },
include: { user: true },
orderBy: { score: 'desc' },
})
Bård
08/19/2021, 1:50 PMBård
08/19/2021, 1:51 PMconst seeds = await prisma.$queryRaw`
SELECT s1.*, count(s2.id)+1 as rank FROM "Seed" s1
JOIN "Seed" s2 on s2.score > s1.score or (s2.score = s1.score AND s2."updatedAt" < s1."updatedAt")
WHERE s1.id = ${id} GROUP BY s1.id
`;
Any way this could've been solved in a prisma query?Bård
08/20/2021, 6:28 AMRyan
08/20/2021, 6:31 AMprisma.$queryRaw
as we don’t support comparing columns of the table yet 🙂