#34551: Case-When aggregation over aggregated fields doesn't work since 4.2
-------------------------------------+-------------------------------------
     Reporter:  Denis Roldán         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  4.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:  orm, aggregate,      |             Triage Stage:
  case, when, field error, bug       |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Denis Roldán):

 Replying to [comment:1 Mariusz Felisiak]:
 > Can you reproduce your issue with Django 4.2.1 (see
 511dc3db539122577aaba71f5a24d65d5adab092)? If yes, please share your
 models.

 Correct. It doesn't work on Django 4.2.1 neither.

 I can reproduce the issue with a test on `aggregation/tests.py`


 {{{
     def test_referenced_group_by_aggregation_over_annotation(self):
         total_books_qs = (
             Book.objects.filter(authors__pk=OuterRef("pk"))
             .order_by()
             .values("pk")
             .annotate(total=Count("pk"))
             .values("total")
         )

         annotated_authors = Author.objects.annotate(
             total_books=Subquery(total_books_qs.annotate(
                     total_books=F("total")
             ).values("total_books")),
             total_books_a=Subquery(total_books_qs.filter(
                 name__istartswith="a"
             ).annotate(
                     total_books_a=F("total")
             ).values("total_books_a")),
         ).values(
             "pk",
             "total_books",
             "total_books_a",
         ).order_by("-total_books")

         totals = annotated_authors.aggregate(
             sum_total_books=Sum("total_books"),
             sum_total_books_a=Sum("total_books_a"),
             a_over_total_rate=Case(
                 When(
                     sum_total_books=0,
                     then=0,
                 ),
                 default=Round(
                     (Sum("total_books_a") / Sum("total_books")) * 100, 2
                 ),
                 output_field=FloatField(),
             ),
         )

         self.assertEqual(totals['sum_total_books'], 3)
         self.assertEqual(totals['sum_total_books_a'], 0)
         self.assertEqual(totals['a_over_total_rate'], 0)

 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34551#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/010701880015f3bc-b9062876-d616-41ec-88e7-6a05f657da92-000000%40eu-central-1.amazonses.com.

Reply via email to