> The actual call I had tried was > Payment.objects.filter(appliedpayment__invoice__location=loc).distinct().ag > gregate(Sum('amount')) > (where amount is a field on Payment). I could do > AppliedPayment...aggregate(...), but unfortunately the AppliedPayment > splits the 'amount' into several values (based on revenue type), which > would mean doing several aggregate calls and summing them, which I've > already done when it seemed appropriate (and I just discovered while > writing this that aggregate can take more than one argument, but I > don't know if that results in a single query).
Multiple aggregates within the same aggregate(), I'm 99% sure, are put into a single query, at least if they're simple and not spanning complex relationships themselves. > For now, when I know > there's generally going to be less than a dozen instances, I've just > done the processing in python, which I'm sure will work fine. > > I was hoping for an elegant solution for when I know there's going to > be several thousand plus instances (or hundreds of thousands after a > year or two of deployment). So in the end, I guess I can work with it, > but mostly I was hoping to find out whether the whole respecting > distinct() thing could be made to happen. Yeah, that's a tricky use case no matter what. I don't think there's a way out of *something* doing some heavy lifting, whether it be python or the db. But as I've seen mentioned many times (often by Malcolm...where's a Malcolm bat-signal when you need one? I'd love to hear his thoughts on this) the ORM isn't designed and can't be expected to handle 100% of cases. This may indeed be a situation where a simple extra() actually *is* the elegant solution. > Dunno if it's worthwhile filing a ticket for it, or if there is one. > Or maybe just a doc ticket for mentioning explicitly that aggregate > ignores distinct. Certainly agree with this. Sorry I couldn't be more help! Regards Scott -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.