This is my problem I want to group by a field which is a result of an annotate, but I can not do it.
from django.db.models import Max, Sum
queryset = queryset.values('period', 'cell').annotate(x=Max('total'))
result = queryset.values('period').annotate(result=Sum('x'))
sample data:
period cell total
1 2 40
1 2 41
2 3 23
1 4 44
First query
queryset = queryset.values('period', 'cell').annotate(x=Max('total'))
period cell x
1 2 41
2 3 23
1 4 44
Expected result in the second query
period result
1 85
2 23
Looking for information, I found that it can be done with subqueries but I can not do it, if someone has another way of doing it, without using a for I also appreciate it.