On Aug 15, 2011, at 6:03 AM, Eduardo wrote: > This I want to do: > 1) I want to find all names that satisfy this query > column_names = session.query(tab.c.name).filter(tab.c.value==354) > > 2) Among those rows I want to find those with the highest id ( all > rows with the same name are fist grouped and only the one with the > highest id is retained the rest is discarded). Each of the names > satisfying the query 1 will be represented with one row having the > highest id: > > column_names = [column_name for (column_name,) in column_names] > > query=sess.query(func.max(tab.c.id),tab.c.name).group_by(*column_names).all()
you'd certainly want to pass Column objects, not strings, to group_by(). These are available from your Table using [t.c[name] for name in column_names]. The string you keep pasting suggests there's only a simple issue of many non-standard characters in your column names and no quoting being applied. For some insight on what it means to pass straight strings to order_by() and group_by() read the "topic" box called "Pros and Cons of Literal SQL" in the tutorial at http://www.sqlalchemy.org/docs/orm/tutorial.html#using-literal-sql. -- 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.