Skip to content
Advertisement

Implementing Two Level Aggregate in PostgreSQL using Sequelize Function for NodeJS App

Hello guys please bear with me here. I’m using PostgreSQL, Sequelize, Express, and NodeJS to create a backend. I’m wondering if these lines of raw query code can be implemented using Sequelize Model findAll function.

First of all, what I am trying to do here is to calculate the total score of these students. Here are some tables and their relations.

If I run line of codes below.

I will get something like this.

Please note that I’m selecting the highest score if more than one quiz with the same id found.

Anyway, I want to implement it using sequelize built in function. What I’ve been trying to do is something like this.

The code above throws an error message which is “aggregate function calls cannot be nested”.

Any kind of help will be appreciated. Thank you.

P.S. I know i can use sequelize.query() function to use the first code block shown, but that’s not the point.

Advertisement

Answer

I find the solution without using any raw query, though I need to get two tables, that is StudentQuiz and Student that coupled by Level. Here is my answer.

With this much complexity, I agree that using raw query by far the best approach for this case.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement