#34798: Using Django 4.2 with MSSQL 2019 Aggregation Containing Subquery Fails
-------------------------------------+-------------------------------------
               Reporter:  haldunk    |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.2
  layer (models, ORM)                |       Keywords:  MSSQL, Aggregation,
               Severity:  Normal     |  Subquery
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 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')
 )
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34798>
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/0107018a2b4d67e2-3a5b5ae0-7fc3-4d8b-abc5-7908ae7a9e63-000000%40eu-central-1.amazonses.com.

Reply via email to