On Tue, 2012-12-11 at 12:41:20 -0800, Jonathan Vanasco wrote: > does anyone have a good reference for this ? > > i'm trying to select > > Table.Column , count(Table.Column) > > however I'm not querying the Table, instead the raw sql is more like > > SELECT a , COUNT(a) FROM ( SELECT FROM TABLE ) AS sq1 > > i've never had to select from a subquery in sqlalchemy before. I can't > wrap my head around this and I can't find relevant docs
Something like the following should do the trick: subq = db_session.query(Model1.field1).\ filter(Model1.field2 == 'bar').subquery() q = db_session.query(subq.c.field1, func.count(subq.c.field1)).\ group_by(subq.c.field1) You may want to look at http://docs.sqlalchemy.org/en/rel_0_8/orm/tutorial.html#using-subqueries for more examples and explanations. -- Audrius Kažukauskas http://neutrino.lt/
pgpm5J0P9Pr7X.pgp
Description: PGP signature