Order_by in sqlalchemy with outer join

wont_compile

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!

javex

First of all you want to make sure that the subquery selects only rows for a particular student. You do that with the correlate method:

score = db.session.query(Scores.id).order_by(Scores.date.desc()).correlate(Students)

This alone does nothing, as you do not access the students. The idea of correlate is that if you use Students on your subquery, it will not add it to the FROM list but instead rely on the outer query providing it. Now you will want to refine your query (the join condition if you wish):

score = score.filter(Students.email == Scores.email)

This will produce a subquery that each time only returns the score for a single student. The remaining question is, if each student has to multiple scores. If so, you need to limit it (if there isn't, you don't need the order_by part from above as well):

score = score.limit(1)

Now you have made sure your query returns a single scalar value. Since you are using the query in a select context you will want to turn it into a scalar value:

students = db.session.query(Students, score.as_scalar()).filter_by(archive=0).order_by(Students.exam_date)

The as_scalar method is a way of telling SQLAlchemy that this returns a single row and column. Because otherwise you could not put it into a select.

Note: I am not 100% sure you need that limit if you put as_scalar. Try it out and expirment. If each student has only one score anyway then you don't need to worry at all about any of that stuff.

A little hint on the way: A Query instance is by itself an iterable. That means as long as you don't print it or similar, you can pass around a query just like a list with the only exception that will really only run on access (lazy) and that you can dynamically refine it, e.g. you could slice it: students[:10] would only select the first 10 students (and if you do it on the query it will execute it as a LIMIT instead of slicing a list, where the former is much more efficient).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Order_by in sqlalchemy with outer join

From Dev

How to use join and order_by with multiple records in SQL/SQLalchemy?

From Dev

SqlAlchemy - Many to Many outer join with conditions on join

From Dev

SqlAlchemy - Many to Many outer join with conditions on join

From Dev

How to execute "left outer join" in SqlAlchemy

From Dev

Sqlalchemy: How to perform outer join with itself?

From Dev

Flask SQLAlchemy order_by relationship

From Dev

Flask-SQLAlchemy Left Outer Join Filtered Query

From Dev

SQLAlchemy many-to-many LEFT OUTER JOIN with sorted list of entities

From Dev

SQLAlchemy order_by string column with int values

From Dev

SQLAlchemy: order_by(None) for joinedload subclause queries?

From Dev

Order by issue when outer joining two tables in sqlalchemy

From Dev

SQLAlchemy query, join on relationship and order by count

From Dev

SQLAlchemy Join Table with a field to order the rows

From Dev

Flask-SqlAlchemy query with order_by and pagination is very slow

From Dev

SQLAlchemy: Override relationship-defined "order_by" in a query

From Dev

SQLAlchemy: How to use order_by based on a calculated value?

From Dev

How to form complex mysql query that has left outer join, aggregate data with group by using SQLAlchemy?

From Dev

OUTER APPLY in SQLAlchemy

From Dev

Combined Left Outer Join and Full Outer Join

From Dev

Right Outer Join to Left Outer join

From Dev

full outer join or right outer join

From Dev

SQL JOIN and LEFT OUTER JOIN

From Dev

Outer join in ActiveRecord for complicated join

From Java

Outer Join on dataframes python

From Dev

Full outer join

From Dev

Complex Outer Join condition

From Dev

LEFT OUTER JOIN with LIMIT

From Dev

Pandas: join with outer product

Related Related

HotTag

Archive