Halvor
09/06/2021, 6:28 PMSELECT
m.*,
d.*,
AVG(r.value) AS rating
FROM "Map" AS m
JOIN "MapData" AS d ON m."id" = d."mapId"
JOIN "MapRate" AS r ON m."id" = r."mapId"
WHERE deleted = false
GROUP BY m.id, d.id
ORDER BY rating DESC
My schema:
enum MapPlatform {
XBOX360 // 1
PS3 // 2
PC // 3
}
model Map {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
user User @relation(fields: [userId], references: [id])
userId Int
platform MapPlatform @default(PC)
uuid String @unique @default(uuid())
title String
size Int
deleted Boolean @default(false)
mapData MapData?
mapRatings MapRate[]
}
model MapData {
map Map @relation(fields: [mapId], references: [id])
mapId Int
id String @id
gamemode Int
battlefieldSize Int
numberPlayers Int
originalCreatorName String
originalCreatedDate DateTime
authorName String
authorDate DateTime
thumbnail String
}
enum MapRateType {
USER
INAPPROPRIATE
}
model MapRate {
map Map @relation(fields: [mapId], references: [id])
mapId Int
user User @relation(fields: [userId], references: [id])
userId Int
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
type MapRateType @default(USER)
value Int
@@id(fields: [mapId, userId])
}
What i want to achieve is to maps sorted by the average of value from the MapRate table, that has a relation with Map table.
I imagine this would require both group by and aggregate functions combined with the findMany() is that even possible with prisma?