## Code

Opened 7 months ago

# Arithmetic operations on aggregates

Reported by: Owned by: debanshuk Database layer (models, ORM) master Normal aggregate, annotate debanshuk Accepted yes yes yes yes no no

### Description

In many cases one may require to do simple arithmetic operations (+, -, *, /) on aggregated (with numbers or other aggregates).
Eg. For model Xyz:

```class Xyz(models.Model):
a = models.IntegerField()
b = models.IntegerField()
```

If I want to find percent ratio of sum of all a and sum of all b, I can write:

```Xyz.objects.aggregate(ratio_percent=Sum('a')/Sum('b')*100)
```

This type of functionality has been implemented in this commit https://github.com/debanshuk/django/commit/722f7a4a17870fe1ffb6c207153d1fca6388a401

More operations (like pow) can also be added.

### comment:1 Changed 7 months ago by debanshuk

• Needs documentation unset
• Needs tests unset
• Patch needs improvement unset

### comment:3 follow-up: ↓ 4 Changed 7 months ago by wim@…

Hi debanshuk,

Wouldn't it be possible to make the same calculation outside of the aggregate? That seems cleaner to me.

Wim

### comment:4 in reply to: ↑ 3 Changed 7 months ago by debanshuk

Hi Wim,

In the example given in the description, it would. And I guess in case of aggregates it would always be possible to make the calculations out side aggregate.
But, in case of annotate it would not. Well, it would certainly be possible, but wouldn’t be cleaner, as one would have to do the calculations for each element in resultant query-set.

Eg. Let modified Xyz be:

```class Xyz(models.Model):
user = models.ForeignKey(User)

a = models.IntegerField()
b = models.IntegerField()
```

Now if I want to find percent ratio of sum of all a and sum of all b of a user, for each user. I may write:

```Xyz.objects.values('user').annotate(ratio_percent=Sum('a')/Sum('b')*100)
```

Currently, to do this (without writing raw SQL), one would have to do something like:

```result = Xyz.objects.values('user').annotate(total_a=Sum('a'), total_b=Sum('b'))
final_result = [{'user': r['user'], 'ratio_percent': r['total_a']/r['total_b']*100} for r in result]
```

Also, If someone want to aggregate over annotation results, like in the example if I want to find maximum of the percentages, I may do:

```Xyz.objects.values('user').annotate(ratio_percent=Sum('a')/Sum('b')*100).aggregate(Max('ratio_percent'))
```

Currently, I would have to do:

```result = Xyz.objects.values('user').annotate(total_a=Sum('a'), total_b=Sum('b'))
final_result = max(r['total_a']/r['total_b']*100 for r in result)
```

In the second one, max calculation in done in python. In the first one it is done in SQL itself.

Debanshu Kundu.

Last edited 7 months ago by debanshuk (previous) (diff)

### comment:5 Changed 7 months ago by timo

Duplicate of #14030?

### comment:6 Changed 7 months ago by debanshuk

Well...not exactly duplicate, as #14030 is for something like this:

```Xyz.objects.aggregate(Sum(F('a')/F('b')*100))
```

but I guess #14030 can be extended to implement functionality of this ticket. In case of annotate something like this can be done:

```Xyz.objects.values('user').annotate(total_a=Sum('a'), total_b=Sum('b')).values('user', ratio_percent=F('total_a')/F('total_b')*100)
```

But, in case of aggregate this wont work as aggregate returns dict (not QuerySet). So, I guess some changes are required for aggregate. Also, both functionalities (operations on aggregates and passing F objects to aggregates) can co-exist (as I think).

Last edited 7 months ago by debanshuk (previous) (diff)

### comment:7 Changed 7 months ago by apollo13

• Needs documentation set
• Needs tests set
• Patch needs improvement set
• Triage Stage changed from Unreviewed to Accepted

### comment:8 Changed 7 months ago by debanshuk

• Owner changed from nobody to debanshuk
• Status changed from new to assigned

### comment:10 Changed 7 months ago by debanshuk

Did amendment in the commit. Updated some conditions in test-cases to make them more reliable.

Version 0, edited 7 months ago by debanshuk (next)

### comment:11 Changed 6 months ago by debanshuk

• Owner debanshuk deleted
• Status changed from assigned to new

### Modify Ticket

Change Properties
Summary: Type: UncategorizedBugNew featureCleanup/optimization contrib.admincontrib.admindocscontrib.authcontrib.commentscontrib.contenttypescontrib.csrfcontrib.flatpagescontrib.formtoolscontrib.humanizecontrib.messagescontrib.redirectscontrib.sessionscontrib.sitemapscontrib.sitescontrib.staticfilescontrib.syndicationcontrib.webdesignCore (Cache system)Core (Mail)Core (Management commands)Core (Other)Core (Serialization)Core (System checks)Core (URLs)Database layer (models, ORM)*.djangoproject.comDocumentationFile uploads/storageFormsGeneric viewsGISHTTP handlingInternationalizationMigrationsPackagingPython 2Python 3Template systemTesting frameworkTranslationsUncategorizedUtilities master1.7-alpha-21.7-alpha-11.61.6-beta-11.6-alpha-11.51.5-rc-21.5-rc-11.5-beta-11.5-alpha-11.41.4-rc-21.4-rc-11.4-beta-11.4-alpha-11.31.3-rc1.3-beta1.3-alpha1.21.2-rc1.2-beta1.2-alpha1.11.1-rc1.1-beta1.1-alpha1.01.0-rc1.0-beta-21.0-beta1.0-alpha-21.0-alpha0.960.95 NormalRelease blocker UnreviewedAcceptedReady for checkinSomeday/Maybe
Action
as new
The owner will be changed from (none) to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.