On Monday, 24 September 2018 15:40:44 UTC+1, Matthew Pava wrote:
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: django...@googlegroups.com [mailto:django...@
googlegroups.com ] 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 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 django-users...@googlegroups.com .
To post to this group, send email to djang...@googlegroups.com.
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
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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/56b3be05-74c9-4fa6-8cf6-dc40b5462b90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment