[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Joril
On 10 Apr, 16:24, Joril wrote: > Now let's see if I can fetch complete instances of C instead of just > the id X-) For the record, my final implementation: sub = s.query(C.d_id, func.max(C.value).label("v")). group_by(C.d_id).subquery() sub2 = s.query(C).join((sub, and_(C.d_id==sub

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Joril
On 10 Apr, 15:54, Mike Conley wrote: > This will teach me to run a test it first, I don't think this is exactly > right, but it should be close. That's ok all the same, thanks for taking the time for posting :) Anyway my current implementation via Query API is sub = s.query(C.d_id, func.max(C.v

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Joril
On 10 Apr, 15:43, Mike Conley wrote: > Try this, has one nested query > sub = session.query(C.id.label('c_id'), >         C.d_id.label('d_id'), >         func.max(C.value).label('c_maxvalue') >         ).group_by(C.d_id).subquery() I tried something like that earlier, but postgreSQL complained t

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Mike Conley
This will teach me to run a test it first, I don't think this is exactly right, but it should be close. -- Mike Conley On Fri, Apr 10, 2009 at 9:43 AM, Mike Conley wrote: > Try this, has one nested query > sub = session.query(C.id.label('c_id'), > C.d_id.label('d_id'), > func.

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Mike Conley
Try this, has one nested query sub = session.query(C.id.label('c_id'), C.d_id.label('d_id'), func.max(C.value).label('c_maxvalue') ).group_by(C.d_id).subquery() q = session.query(D.id,sub.c.c_id,sub.c.c_maxvalue).outerjoin(sub) print q for row in q: print 'D-id:%s C-id:%

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread Joril
On 10 Apr, 12:52, a...@svilendobrev.com wrote: > that's what i have in bitemporal queries. > u need a groupby and subquery/ies. I see, thanks for your hint! I tried to do it with bare SQL via pgadmin, and I ended up with select d.id, c.id from d left outer join ( select c.* from c join (

[sqlalchemy] Re: Query with outer join and function

2009-04-10 Thread az
that's what i have in bitemporal queries. u need a groupby and subquery/ies. something along subs = select( [C.id.label('cid'), C.d_id.label('did'), func.max(C.value).label('cvalue')] ).group_by( C.id ) giving the max cid/cvalues, and then somehow join Ds with that. D.query( ...).filte