#34798: Using Django 4.2 with MSSQL 2019 Aggregation Containing Subquery Fails
-------------------------------------+-------------------------------------
     Reporter:  Haldun Komsuoglu     |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  invalid
     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
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

 * status:  new => closed
 * resolution:   => invalid


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)')
>
> 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')
> )
> }}}

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.FloatField()

 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')
 )
 }}}

--

Comment:

 Thanks for the report, however I cannot reproduce it using any builtin
 backend. You should try to report it to the issue tracker of the 3rd-party
 database backend that you're using.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34798#comment:2>
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/0107018a2b5e33d5-6583f52c-eb60-4080-bde7-903d85ead74d-000000%40eu-central-1.amazonses.com.

Reply via email to