[sqlalchemy] Re: How to refer to fields in nested queries?
If you don't use a subquery, you can use the `desc("column")` in `sqlalchemy`. (there is an `asc()` as well). Below I use `label('name')` to specify the result column name for computed field, then order by it. results = dbSession.query( model.Foo.a, sqlalchemy.func.count(Model.Foo.b).label('count_b') )\ .group_by(model.Foo.a)\ .order_by(sqlalchemy,desc('count_b')\ .all() If you are using a subquery for additional processing or joins, you can use `label()` to specify the name for the computed column, then access that column as an attribute on the `.c` columns attribute of the subquery object. inner_query = dbSession.query( model.Foo.a, sqlalchemy.func.count(Model.Foo.b).label('count_b') ) inner_query = inner_query.subquery('query_foo') results = dbSession.query(inner_query.c.count_b.label('count_results')).all() -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Re: How to refer to fields in nested queries?
> If you are using a subquery for additional processing or joins, > you can use `label()` to specify the name for the computed column, > then access that column as an attribute on the `.c` columns attribute of the > subquery object. Thanks for your information. > inner_query = dbSession.query( >model.Foo.a, >sqlalchemy.func.count(Model.Foo.b).label('count_b') > ) > inner_query = inner_query.subquery('query_foo') > results = > dbSession.query(inner_query.c.count_b.label('count_results')).all() Another part of the desired data analysis is working here now. Will it become easier to recognise the relevant relationships from the corresponding programming interface documentation? Regards, Markus -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Re: How to refer to fields in nested queries?
On Tuesday, December 8, 2015 at 12:14:46 PM UTC-5, SF Markus Elfring wrote: > > Will it become easier to recognise the relevant relationships > from the corresponding programming interface documentation? > You should read through the tutorial/narrative documentation on the ORM and "Core", as well as the FAQ. That will familiarize you a lot with where to find the information. The SqlAlchemy library is incredibly robust and large; even after years of usage people find new features. -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.