I have a table named grade
with 4 columns: student_id
, subject_id
, grade
, and date
.
student_id | subject_id | grade | date |
---|---|---|---|
Jenny | math | 90 | 2021-12-08 |
Susan | math | 60 | 2021-12-08 |
Jenny | math | 80 | 2021-12-07 |
Susan | math | 50 | 2021-12-07 |
Jenny | science | 80 | 2021-12-08 |
Susan | science | 90 | 2021-12-08 |
Jenny | science | 76 | 2021-12-06 |
Susan | science | 85 | 2021-12-06 |
I would like to select all rows with only the last grade of each student for each subject. Basically, I want to select all rows with unique student_id
, subject_id
, like this:
student_id | subject_id | grade | date |
---|---|---|---|
Jenny | math | 90 | 2021-12-08 |
Susan | math | 60 | 2021-12-08 |
Jenny | science | 80 | 2021-12-08 |
Susan | science | 90 | 2021-12-08 |
Here is what I have tried:
await Grade.findAll({
attributes: ['student_id', 'subject_id', 'grade', 'date'],
raw: true,
group: ['student_id', 'subject_id']
})
However, I get the following error:
SequelizeDatabaseError: column "grade.grade" must appear in the GROUP BY clause or be used in an aggregate function