[sqlalchemy] Re: select where field=max(field)

2008-11-11 Thread John Hunter
On Tue, Nov 11, 2008 at 4:31 AM, King Simon-NFHD78 <[EMAIL PROTECTED]> wrote: > Which is pretty much the query we wanted, apart from the names. I hope > it works in your original example as well! This worked great -- and I learned a bunch of useful sql and sqlalchemy tricks along the way. Many

[sqlalchemy] Re: select where field=max(field)

2008-11-11 Thread King Simon-NFHD78
> -Original Message- > From: sqlalchemy@googlegroups.com > [mailto:[EMAIL PROTECTED] On Behalf Of John Hunter > Sent: 11 November 2008 01:54 > To: sqlalchemy@googlegroups.com > Subject: [sqlalchemy] Re: select where field=max(field) > > > On Mon, Nov 10, 2008

[sqlalchemy] Re: select where field=max(field)

2008-11-10 Thread John Hunter
On Mon, Nov 10, 2008 at 11:10 AM, Michael Bayer <[EMAIL PROTECTED]> wrote: > you need an extra tuple on the join, query.join((q1, s.s==q1.c.s)) This gets past the syntax error, but does not produce the right results. I had to take some time off today to work on other problems, but am now return

[sqlalchemy] Re: select where field=max(field)

2008-11-10 Thread John Hunter
On Mon, Nov 10, 2008 at 10:05 AM, King Simon-NFHD78 <[EMAIL PROTECTED]> wrote: > Actually, the section after that (Using Subqueries) probably does > something very close to what you want. What's the result of these lines: > > q1 = (session.query(Snapshot.strategy, Snapshot.symbol, sum_pnl) >

[sqlalchemy] Re: select where field=max(field)

2008-11-10 Thread Michael Bayer
On Nov 10, 2008, at 12:08 PM, John Hunter wrote: > > On Mon, Nov 10, 2008 at 10:05 AM, King Simon-NFHD78 > <[EMAIL PROTECTED]> wrote: > >> Actually, the section after that (Using Subqueries) probably does >> something very close to what you want. What's the result of these >> lines: >> >> q1 =

[sqlalchemy] Re: select where field=max(field)

2008-11-10 Thread King Simon-NFHD78
> -Original Message- > From: sqlalchemy@googlegroups.com > [mailto:[EMAIL PROTECTED] On Behalf Of John Hunter > Sent: 10 November 2008 15:29 > To: sqlalchemy@googlegroups.com > Subject: [sqlalchemy] Re: select where field=max(field) > > > On Mon, Nov 10,

[sqlalchemy] Re: select where field=max(field)

2008-11-10 Thread John Hunter
On Mon, Nov 10, 2008 at 8:53 AM, King Simon-NFHD78 <[EMAIL PROTECTED]> wrote: > It should be fairly easy to build that query with SA's underlying > expression language. I'm not certain how to do it through session.query, > but I'm sure it's possible. The snippet you posted does do what I want wh

[sqlalchemy] Re: select where field=max(field)

2008-11-10 Thread King Simon-NFHD78
> -Original Message- > From: sqlalchemy@googlegroups.com > [mailto:[EMAIL PROTECTED] On Behalf Of John Hunter > Sent: 10 November 2008 14:07 > To: sqlalchemy@googlegroups.com > Subject: [sqlalchemy] Re: select where field=max(field) > > > On Mon, Nov 10,

[sqlalchemy] Re: select where field=max(field)

2008-11-10 Thread John Hunter
On Mon, Nov 10, 2008 at 4:33 AM, King Simon-NFHD78 <[EMAIL PROTECTED]> wrote: > I'm no SQL expert, so please take this with a pinch of salt, but as far > as I know, conditions in the 'WHERE' clause of an SQL statement are > applied BEFORE any grouping, so you can't use grouping functions (such >

[sqlalchemy] Re: select where field=max(field)

2008-11-10 Thread King Simon-NFHD78
> -Original Message- > From: sqlalchemy@googlegroups.com > [mailto:[EMAIL PROTECTED] On Behalf Of John Hunter > Sent: 08 November 2008 05:09 > To: sqlalchemy@googlegroups.com > Subject: [sqlalchemy] Re: select where field=max(field) > [SNIP] > Here is a quer

[sqlalchemy] Re: select where field=max(field)

2008-11-07 Thread John Hunter
On Fri, Nov 7, 2008 at 3:57 PM, Michael Bayer <[EMAIL PROTECTED]> wrote: > > Theres a good tutorial on the topic of GROUP BY from a SQL > perspective, here: > > http://weblogs.sqlteam.com/jeffs/jeffs/archive/2007/07/20/60261.aspx > > in this case you probably want > query.filter(Snapshot.totalqty=

[sqlalchemy] Re: select where field=max(field)

2008-11-07 Thread Bobby Impollonia
If you are okay with only getting one record in the case of ties you can do session.query(Snapshot).order_by(Snapshot.totalqty.desc()).first() On Fri, Nov 7, 2008 at 12:22 PM, John Hunter <[EMAIL PROTECTED]> wrote: > > I am having trouble writing a sqlalchemy query which selects all rows > where

[sqlalchemy] Re: select where field=max(field)

2008-11-07 Thread Michael Bayer
Theres a good tutorial on the topic of GROUP BY from a SQL perspective, here: http://weblogs.sqlteam.com/jeffs/jeffs/archive/2007/07/20/60261.aspx in this case you probably want query.filter(Snapshot.totalqty==func.max(Snapshot.totalqty).select()). On Nov 7, 2008, at 3:22 PM, John Hunter