#28296: Add support for aggregation through subqueries
-------------------------------------+-------------------------------------
     Reporter:  László Károlyi       |                    Owner:  B
                                     |  Martsberger
         Type:  New feature          |                   Status:  assigned
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by B Martsberger):

 Matt, I'm not sure your use case will be improved by this ticket due to
 the need to group by a computed field. But it also looks like it's not
 necessary to subclass Subquery with a new template.


 {{{
 event_day_subquery = Subquery(
     Event.objects.annotate(day=TruncDay('timestamp'))
          .values('project_id', 'day')
          .annotate(cnt=Count(Value(1)))
          .values('cnt')
          .filter(project_id=OuterRef('id'))

 Player.objects.annotate(day_count=event_day_count).values('id',
 'day_count')
 }}}

 Produces SQL like

 {{{
 SELECT "player_player"."id",
     (SELECT COUNT(1) AS "cnt"
      FROM "player_event" U0
      WHERE U0."player_id" = "player_player"."id"
      GROUP BY U0."player_id",
               DATE_TRUNC('day', U0."timestamp" )) AS "day_count"
 FROM "player_player"
 }}}

 Replying to [comment:17 Matt Hegarty]:

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28296#comment:18>
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/065.9e7921a4f7a3687d7cd65d71a9a0b8be%40djangoproject.com.

Reply via email to