10
SELECT * 
FROM product_stocks 
WHERE detected_date = (
                         SELECT MAX(detected_date) 
                         FROM product_stocks 
                         WHERE id = 18865
                      ) 
      AND id = 18865;

Having lots of trouble converting this to SQLAlchemy query string. What's the most efficient way?

2
  • What do you mean by "most efficient way"? Are you trying to optimize the query before you even know how to build it? Commented Dec 2, 2014 at 23:03
  • Also, are you using the SQLAlchemy ORM, or just the query expression language? Commented Dec 2, 2014 at 23:12

3 Answers 3

11

You can use from_statement to execute the raw SQL-Query and fetch it in a SQL-Alchemy Object. This helps when it's easier to write plain SQL then SQLAlchemy Syntax.

Session.query(YourClass).from_statement(text('''SELECT * FROM product_stocks 
WHERE detected_date = (SELECT MAX(detected_date) FROM product_stocks WHERE id = 18865)
AND id = 18865;''')).all()
Sign up to request clarification or add additional context in comments.

4 Comments

Would you know how to do this from classic SQLAlchemy?
didn't used SQLAlchemy in a long time. But as far as I remember this was the way to do it. Not sure what you mean with classic SQL alchemy. But maybe you mean the short handle for a specific Model which only replaces the Session.query(class). Rest should be the same. And it is only about on what Object you want to bind the data. But I could be wrong because as I said I didn't use it in a long time.
In case someone runs into problems with this, I found the following answer to be useful stackoverflow.com/questions/23344728/…
exactly what I needed.
3

Below will recreated the SQL you asked for:

_id = 18865
T = aliased(ProductStock, name="T")
T1 = aliased(ProductStock, name="T1")
subquery = (
    session.query(func.max(T1.detected_date).label("detected_date"))
    .filter(T1.id == _id)
    # .filter(T1.id == T.id)  # @note: i prefer this one to the line above
    .as_scalar()
)
qry = (
    session.query(T)
    .filter(T.detected_date == subquery)
    .filter(T.id == _id)
)

Is this the most efficient way to accomplish what you want? - I am not so sure, but not enough information

Comments

3

With Core SQLAlchemy 1.4/2.0:

from sqlalchemy import text, select, column
sql = 'SELECT foo FROM bar'
sql = text(sql)
sql = sql.columns(column('foo'))  # This let's it be used as a subquery

sel = select(sql.selected_columns.foo).select_from(sql.subquery())

joined = sel.outerjoin(baz_t, baz_t.foo==sel.c.foo)

final = select(sel.c.foo).select_from(joined)

With Core SQLAlchemy < 1.4:

sql = 'SELECT foo FROM bar'
sql = text(sql)
sql = sql.columns()  # This let's it be used as a subquery

sel = select(['foo']).select_from(sql)
# I needed this for a complex query or else columns would be ambiguous
sel = sel.alias('sel')  

joined = sel.outerjoin(baz_t, baz_t.foo==sel.c.foo)

final = select([sel.c.foo]).select_from(joined)

Note that the columns() is necessary, and the alias() is helpful if the query is complex.

The following text documentation is helpful.

3 Comments

just tried this out, did not work for me
@Saikathalder You are probably using SQLAlchemy 2.0. This answer was written 5 years ago. I've added an untested update. Perhaps you should mention what error you receive next time?
Thank you for your comment. It did not exactly work for me, but I found solution by playing around from your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.