Hi, I'm trying to convert the following query to sqlalchemy:
SELECT cg.id, cg.name cg.status, history.action, history.timestamp FROM customergame cg JOIN history ON history.id = (SELECT id FROM history h WHERE cg.id = h.customergame_id ORDER BY timestamp DESC LIMIT 1) ORDER BY gamename ASC Essentially to select the customergame entry and the last entry in the history table for that customer game. I've a number of ways to try and archive this, however, nothing so seems to work. For example: subq = db.session.query(History.id).filter(Customergame.id==History. customergame_id).order_by(History.timestamp).limit(1).subquery() db.session.query(Customergame).join(History, History.id==subq.c.id) Which produces: SELECT customergame., FROM customergame JOIN history ON history.id = anon_1.id Which in turn leads to a exception when it executes: ProgrammingError: (ProgrammingError) missing FROM-clause entry for table "anon_1" Has anybody got any ideas of how to achieve this type of query? Many thanks in advance! -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
