sagar lama
06/23/2022, 4:56 AMmodel Course {
id Int @id @default(autoincrement())
// ...
fee Fee[]
@@map("courses")
}
model Fee {
id Int @id @default(autoincrement())
course_id Int
course Course @relation(fields: [course_id], references: [id])
feeItems FeeItems[]
// ...
@@map("fees")
}
model FeeItems {
id Int @id @default(autoincrement())
amount Float
number_of_installment Int
fee_id Int
fee Fee @relation(fields: [fee_id], references: [id])
// ...
@@map("fee_items")
}
I'm trying filter course based on the sum of fee items
What's the best way to do it?Sabin Adams
07/06/2022, 7:34 PM$queryRaw function and run a query similar to this to get these fine-grained results:
async function findCourseByAmount(amount: number) {
await prisma.$queryRaw`
SELECT "Course".*
FROM "Course"
LEFT JOIN "Fee" ON "Fee".course_id = "Course".id
LEFT JOIN "FeeItems" ON "FeeItems".fee_id = "Fee".id
GROUP BY "Course".id
HAVING SUM("FeeItems".amount) > ${amount}
`;
}
(This grabs all of the courses whose sum of fees is GREATER than a provided amount)Sabin Adams
07/19/2022, 10:26 PM