Do you know a good way to do what I am trying to do? I can write a
quick for loop to do the work, but I was hoping to offload some of the
work to the database since it is much faster than a Python for loop.

I have a set of say 10,000 Product models. I want to get a list of the
100 with the highest prices, which I currently do with this query:

Product.objects.order_by("-price")[:100]

I then want to know the maximum and minimum prices within that list. I
was hoping I could use aggregation and let the database find the
maximum and minimum for me, but that doesn't work as Alex said.

On Mar 3, 10:08 am, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Tue, Mar 3, 2009 at 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.
>
> > Is this an improper use of aggregation, or am I doing something wrong
> > with my query?
>
> Before an aggregation is preformed all limits are removed, so you are seeing
> expected behavior.  I can't remember why this behavior exists though :/
>
> 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