开发者

Django -- Aggregates Sums in postgreSQL don't use DISTINCT. Is this a bug?

Is this a bug? I'm not good at SQL, but it looks like the aggregate function doesn't respect the distinct() passed to the queryset.

distinct_queryset = MyModel.objects.filter(reverse_relationship__icontains='foo').distinct()

iterated_total = 0
for item in distinct_queryset:
    iterated_total += item.total

aggregate = queryset.aggregate(Sum('total'))

aggregate is Decimal('42201.20')

iterated_total is Decimal('38212.20')

aggre开发者_高级运维gate is not total


Update:

I finally tested it with debug=true and connections.queries.

on somequery.distinct() I get:
'sql': 'SELECT DISTINCT ............ WHERE....LIKE FOO

on aggregate of a distinct queryset I get:
'sql': 'SELECT .......... WHERE...LIKE FOO

After searching google, I did find http://groups.google.com/group/django-users/browse_thread/thread/87cc286019c7d57c

I managed in this way:

subquery = Father.objects.filter(sons_in=[adam, bernard]) Father.objects.filter(pk_in=subquery).aggregate(Sum('age'))

This gives the correct result. Is there a better way to do so? (maybe more efficient?)

I will keep adding via python from my gut feeling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜