#28940: Annotate SUM aggregation value as field
-------------------------------------+-------------------------------------
Reporter: Vasiliy Maryutenkov | Owner: nobody
Type: Uncategorized | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):
* status: new => closed
* resolution: => duplicate
Comment:
If I understand you correctly you're after something along
{{{#!python
queryset = Model.objects.annotate(
total=Subquery(Model.objects.aggregate(Sum('field')),
percent=F('field')/F('total'),
)
}}}
But that won't be possible until #28296 gets fixed.
In the mean time you can work around the issue by performing two queries.
A first one to retrieve the aggregated sum and a second one to annotate
the percentage on rows.
{{{#!python
total = Model.objects.aggregate(total=Sum('field'))['total']
queryset = Model.objects.annotate(percentage=F('field')/total)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28940#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" 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].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.601838a33c76dbe1e68dc7a25418d23b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.