> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of rajasekhar911
> Sent: 17 November 2009 11:32
> To: sqlalchemy
> Subject: [sqlalchemy] Re: column label and order by
> 
> 
> anyone??
> 
> On Nov 14, 6:48 pm, rajasekhar911 <[email protected]> wrote:
> > Hi guys,
> >
> > how do i apply order by on a column with a label.
> > My requirement is like this
> >
> > class x
> >   id,
> >   amount,
> >   date
> >
> > i have to group based on id and take sum of amount within a date
> > range.
> >
> > i am applying a label to sum of amount
> > now how do i order based on that so that i can get top 5 ..
> >
> > session.query( func.sum(x.amount).label('tot_amount'), x.id ).
> > filter(x.date>fromdate).filter(x.date<todate).
> > .group_by(x.id)
> > .order_by(?????)
> > .limit(5)
> >
> > thanks.

How about (untested):

tot_amount = func.sum(x.amount).label('tot_amount')
session.query(tot_amount, x.id).
filter(x.date>fromdate).filter(x.date<todate).
.group_by(x.id)
.order_by(tot_amount)
.limit(5)

Simon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to