Jacob Kaplan-Moss wrote:
> No, I think not -- I think that syntax (``queryset.groupby(field).max()``)
> actually looks like the best proposal for aggregates I've seen thus far...
>
> Thoughts, anyone?
>
> Jacob

I think it quickly gets more complicated than that syntax would
support. For example, how would you ask for more than one aggregate
value in that syntax? My common use case is grouping a bunch of
financial positions, where the SQL would look something like:

   select account, count(*), sum(quantity), sum(total_pnl) from
position
   group by account

Would I have to call queryset.groupby(account) three times: once for
count(), once for sum(quantity) and once for sum(total_pnl)?

And what exactly does queryset.groupby() return? In my case, if account
is a ForeignKey from a Position model to an Account model, can I
dereference fields from the result?

  account_summary =
Position.objects.filter(date=today).groupby(account)
  for summary on account_summary:
    print summary.name        ### would this work? Is this the name
property of an Account?

And how would I dereference the aggregate fields in the groupby
results? By index? (is account_summary[0][2] the quantity sum of the
first account summary row?)

I've run into all of these issues (multiple aggregate columns,
dereferencing model relations, aggregate alias names) in playing around
with this and I think they are all problems you run into quickly that
make a solution rather complicated.

My idea was a queryset.groupby() could return some sort of dynamic
Django model class where the attributes where the aggregated fields
plus the fields you were grouping by and if you were grouping by a
relation field, it would magically work like any other model relation.

But I don't know how complicated that would be and I haven't thought of
a syntax that works nicely for the more complex cases.

-Dave


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

Reply via email to