On Wed, Mar 4, 2009 at 9:24 AM, Waylan Limberg <way...@gmail.com> wrote:

>
> On Mar 3, 8:55 am, Ross <real...@gmail.com> wrote:
> > I have started using aggregation, but it seems to ignore any slicing I
> > do on a QuerySet before calling aggregate. This is what I'm doing:
> >
> > Product.objects.order_by("-price")[:100].values("price").aggregate(Max
> > ("price"), Min("price"))
> >
> > I want the maximum and minimum price for the products with the top 100
> > largest prices. The aggregate function, however, returns the maximum
> > and minimum price for all Products--it ignores the [:100] slice.
> >
>
> You do realize that because your ordering by "price" your min and max
> will be the first and last items in the returned QuerySet, don't you?
> So something like:
>
> >>> qs = Product.objects.order_by("-price")[:100]
> >>> max = qs.values("price")[0]
> >>> min = qs.values("price")[-1]
>
> The above is untested, but you should get the idea. Yeah, it would
> seem that aggregation would be nice for this, but if this works...
>  >
>

But that requires actually pulling in all the records, not a big deal when
it's 100, but if it's more, and network latency is an issue you don't want
to do that.  Plus I don't think negative slicing works in SQL.

Alex
-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
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