#34798: Using Django 4.2 with MSSQL 2019 Aggregation Containing Subquery Fails
-------------------------------------+-------------------------------------
Reporter: Haldun Komsuoglu | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: MSSQL, Aggregation, | Triage Stage:
Subquery | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Haldun Komsuoglu:
Old description:
> Aggregation in a query employing a subquery expression fails with the
> following error:
>
> ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL
> Server][SQL Server]Cannot perform an aggregate function on an expression
> containing an aggregate or a subquery. (130) (SQLExecDirectW)')
>
> To generate the error for following example can be used:
>
> {{{
> #!python
> class Exchange(models.Model):
> date = models.DateField()
> value = models.FloatField()
>
> class Invoice(models.Model):
> date = models.DateField()
> gross = models.FloatFlied()
>
> exchange =
> Subquery(Exchange.objects.filter(date__lte=OuterRef('date')).order_by('-date').values('value')[:1])
>
> Invoice.objects.annotate(
> exchange=exchange,
> gross_currency=F('gross') / F('exchange')
> ).aggregate(
> avg_gross_currency=Avg('gross_currency')
> )
> }}}
New description:
Aggregation in a query employing a subquery expression fails with the
following error:
ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL
Server][SQL Server]Cannot perform an aggregate function on an expression
containing an aggregate or a subquery. (130) (SQLExecDirectW)')
However, the same query __works__ in Django 4.1.10.
To generate the error for following example can be used:
{{{
#!python
class Exchange(models.Model):
date = models.DateField()
value = models.FloatField()
class Invoice(models.Model):
date = models.DateField()
gross = models.FloatFlied()
exchange =
Subquery(Exchange.objects.filter(date__lte=OuterRef('date')).order_by('-date').values('value')[:1])
Invoice.objects.annotate(
exchange=exchange,
gross_currency=F('gross') / F('exchange')
).aggregate(
avg_gross_currency=Avg('gross_currency')
)
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/34798#comment:1>
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018a2b50eed0-c7995835-42b6-4815-897f-32ea4ba7fd83-000000%40eu-central-1.amazonses.com.