Based on this schema: ```model Seed { id ...
# orm-help
b
Based on this schema:
Copy code
model 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?
r
@Bård 👋 Something like this you mean?
Copy code
await prisma.seed.findMany({
  where: { score: { gte: 400 } },
  include: { user: true },
  orderBy: { score: 'desc' },
})
b
Hey @Ryan, thanks for the reply.
I wrote the statement I needed in raw SQL:
Copy code
const 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?
This query gives me the rank of the game based on the score and filtered by the game id (id field in the query). Thing is I need to also return the result of the preceding rank and the ensuing row. Lets say the game id I'm sending is rank 99 in the highscore list. What i want is rank 98 and rank 100 as well as my current game rank 99. Sorry if this is moving more into general SQL-theory and maybe not that much Prisma, I will understand if I have to seek help else where 🙂 Cheers!
r
The query that you posted above would have to be run via
prisma.$queryRaw
as we don’t support comparing columns of the table yet 🙂