On May 16, 2007, at 7:51 AM, ml wrote:

>
> Hi!
>
> Example:
> I have a table "pictures" with columns "format", "size", and  
> "color". I
> want to get items grouped by "color":
>
> res = session.query(Picture).group_by(Picture.c.color).select()
>
> but PostgreSQL reports that format and size must be in the GROUP BY or
> in a aggregate function (because it doesn't know which values it can
> pick). Can I tell SA that it should apply e.g. MIN() function to all
> columns not included in the GROUP BY?

not directly with an ORM query, since the ORM maps objects to the  
individual rows of a selectable, not aggregates of that selectable  
(in particular PK here is unclear? is the PK composite of those other  
cols or is a surrogate?).  youd have to somehow correlate a primary  
key value from your MIN query to the straight "Picutre" table, such as:

palias = picture_table.alias('pa')
session.query(Picture).select(exists([1], group_by=[palias.c.color],  
having=func.min(palias.c.id)==pictures.c.id))



--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to