On Tue, Jan 27, 2009 at 10:55 AM, Glenn Maynard <glennfmayn...@gmail.com> wrote:
>
> How are aggregates grouped on anything other than a simple column?

I'm a little bit confused as to what you think is happening here -
what do you mean by "other than a simple column"? The default
aggregate grouping is effectively by object instance; the
implementation means that every non-aggregate field on the model that
is returned by the SELECT is included in the GROUP BY.

> db/sql/query.py set_group_by suggests that this might work, to (for
> example) group together dollar amounts:

That block of code is internal API - if you choose to use it, you do
so at your own risk, and backwards compatibility is not guaranteed.
set_group_by() is an internal setup call; I doubt it will do anything
useful in your case.

The public API for aggregates consists of annotate(), aggregate() and
the interaction of those calls with values().

> Table.objects.extra(select={"dollars": "cents/100"}).values
> ("dollars").aggregate(...)
>
> That doesn't throw an error (it does if the extra() isn't there), but
> it doesn't group as expected, either.  (For some reason, it adds a
> GROUP BY containing every field in Table.)

The reason every field in the table is included is because that's how
GROUP BY works - any field that is in the query that isn't an
aggregate needs to be included in the group by. When you call
Table.objects...., this implies SELECT Table.id, Table.name, ... at
the database level. Some other fields are included in the GROUP BY to
accomodate the needs of order_by() and select_related().

The only exception to this is MySQL; MySQL allows for grouping by
primary key, which is an optimization for the simple case of grouping
by every non-aggregate field on a model.

I'd like to provide some more concrete advice, but it's not exactly
clear what you're trying to achieve. If you describe the actual
problem you are trying to solve, what you have tried, and the errors
or unexpected results you have seen, we might be able to provide
better advice.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to