Order_by in sqlalchemy with outer join
I have the following sqlalchemy queries:
score = Scores.query.group_by(Scores.email).order_by(Scores.date).subquery()
students = db.session.query(Students,
score.c.id).filter_by(archive=0).order_by(Students.exam_date).outerjoin(score,
Students.email == score.c.email)
And then I render the things with:
return render_template('students.html', students=students.all())
Now, the issue is that I want the last score for a student to be
displayed, as there are many of them corresponding to each user. But the
first one seems to be returned. I tried some sortings and order_by on the
first query, score, but without success.
How can I affect and pick only one latest result from the "score" to be
paired with the corresponding row in "students"?
Thanks!
No comments:
Post a Comment