As I thought about it more, I came up with something similar to what
both of you suggested. Django's query language has made my SQL
knowledge suffer a bit--your explanation about why the limit won't
work makes sense, Jeff.

Thanks a lot for the suggestions.

On Mar 3, 10:39 am, Jeff FW <jeff...@gmail.com> wrote:
> This time I didn't, apparently, read what you wanted exactly.  Here I
> was, talking about sums, when all you want are the minimum and
> maximum.  Apparently, I shouldn't answer mailing lists in the
> morning.
>
> Anyway, *most* of what I said holds true, as min and max work pretty
> much the same way as sum--in Python, and in SQL.
>
> On Mar 3, 11:36 am, Jeff FW <jeff...@gmail.com> wrote:
>
> > Responded too quickly :-)
>
> > If you're already getting a list of the top 100 products (and
> > displaying them, I assume, in a loop,) then totalling up the prices in
> > Python really won't hurt at all.  I'd only suggest going with my
> > *previous* suggestion if you *weren't* already fetching the top 100
> > products.
>
> > -Jeff
>
> > On Mar 3, 11:34 am, Jeff FW <jeff...@gmail.com> wrote:
>
> > > The behavior is there because you can't limit an aggregate function in
> > > (AFAIK) SQL in that way.  It just doesn't make sense--what would this
> > > actually mean?
>
> > > select sum(price) from product limit 100;
>
> > > Really, you'd be limiting the number of *rows* of sum returned, which,
> > > unless you're using GROUP, is going to be 1.  To do what you want,
> > > you'd need a subselect, which (correct me if I'm wrong,) you'd have to
> > > manually write in SQL.  Like so:
>
> > > select sum(price) from (select price from product order by price desc
> > > limit 100) as q;
>
> > > (That's postgres; your DB may vary.)
>
> > > I could be completely off-base here, as I haven't delved very far into
> > > the aggregate code, but from what I can tell, this is the case.
>
> > > -Jeff
>
> > > On Mar 3, 11: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