[sqlalchemy] Re: Single row of a One to Many query

2009-09-03 Thread Noufal
On Jul 24, 7:19 pm, Michael Bayer mike...@zzzcomputing.com wrote: [..] finding a list of objects and the most recent/highest/somethingest related item requires joining to a subquery, where the subquery selects the func.max(desiredfield) and GROUP BY's the columns that relate the rows to the

[sqlalchemy] Re: Single row of a One to Many query

2009-09-03 Thread Mike Conley
On Thu, Sep 3, 2009 at 8:05 AM, Noufal nou...@gmail.com wrote: stmt = session.query(Order.table.c.client_id,func.max (Order.table.c.date).label('latest_order')).group_by (Order.table.c.date).subquery() I think your group_by needs to be Order.table.c.client_id to get latest order per

[sqlalchemy] Re: Single row of a One to Many query

2009-09-03 Thread Noufal
On Sep 3, 6:15 pm, Mike Conley mconl...@gmail.com wrote: On Thu, Sep 3, 2009 at 8:05 AM, Noufal nou...@gmail.com wrote:    stmt = session.query(Order.table.c.client_id,func.max (Order.table.c.date).label('latest_order')).group_by (Order.table.c.date).subquery() I think your group_by

[sqlalchemy] Re: Single row of a One to Many query

2009-07-24 Thread Mike Conley
It sounds like you need a subquery that finds last order then construct a query joining clients and orders to the subquery. http://www.sqlalchemy.org/docs/05/ormtutorial.html?highlight=subquery#using-subqueries Something like this: class Client(Base): __tablename__ = 'client' id =

[sqlalchemy] Re: Single row of a One to Many query

2009-07-24 Thread Michael Bayer
Noufal wrote: Hello everyone, I've been using sqlalchemy with elixir for a legacy project for a while now and recently needed to write some more than trivial queries. I have the default elixir generated mappers but using only them forces me to do some data processing in my app rather