Hi David,
Performing multiple annotations on a queryset is not the same as combining 
multiple aggregations.
Saying that, if you truly are getting wrong results, you could try using the 
Window functions or the Subquery object.
https://docs.djangoproject.com/en/2.1/ref/models/database-functions/#window-functions
https://docs.djangoproject.com/en/2.1/ref/models/expressions/#subquery-expressions


From: [email protected] [mailto:[email protected]] On 
Behalf Of David
Sent: Monday, September 24, 2018 9:36 AM
To: Django users
Subject: Multiple annotations in one query

Hi

I understand that performing multiple annotations on a queryset will produce 
incorrect results, I was wondering how anyone else gets around this?

"Combining multiple aggregations with annotate() will yield the wrong 
results<https://code.djangoproject.com/ticket/10060> because joins are used 
instead of subqueries"

My query for example:

        data = Client.objects.filter(
                case=self.kwargs['case_pk']
            ).select_related(
            'case', 'inheritance',
            ).prefetch_related(
                'children_set', 'stepchildren_set',
                'grandchildren_set', 'property_set',
                'investment_set', 'insurance_set',
                'liability_set', 'lumpsumdeathinservice_set',
            ).annotate(
                property_sum=Sum('property__value',),
                investment_sum=Sum('investment__value'),
                insurance_sum=Sum('insurance__value'),
                liability_sum=Sum('liability__value'),
                lumpsumdeathinservice_sum=Sum('lumpsumdeathinservice__value'),
            ).order_by(
                'last_name', 'first_name'
            )


I need to get the sum'd values.

Will I have to query each model individually and annotate it? and then somehow 
merge the querysets?

Thanks
--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/963bba41-6bcb-43b5-bd34-9ae4b9f709d5%40googlegroups.com<https://groups.google.com/d/msgid/django-users/963bba41-6bcb-43b5-bd34-9ae4b9f709d5%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/db4521c795cb429fa03c439c13b5bc33%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.

Reply via email to