On Jan 7, 2013, at 9:19 PM, Ji Zhang wrote: > Hi, > > My SQL is like "select host, count(*) as cnt from tbl group by host order by > cnt desc" > > How to achieve this using ORM? > > session.query(Tbl.host, > func.count('*').label('cnt')).group_by(Tbl.host).order_by(???) > > I don't wanna type again func.count... in order_by(). >
count = func.count('*').label('cnt') query(Tbl.host, count).group_by(Tbl.host).order_by(count.desc()) or query(Tbl.host, func.count('*').label('cnt').group_by(Tbl.host).order_by(desc('cnt')) -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.