id summary reporter owner description type status component version severity resolution keywords cc stage has_patch needs_docs needs_tests needs_better_patch easy ui_ux 26045 Annotate with conditional aggregation includes non-relevant fields in GROUP BY clause mark88 nobody "I'm using Django 1.8.6 and conditional aggregates. {{{ objects = self.get_queryset().annotate(sent_count= Sum( Case(When(messages__date_sent__gte=date_from, then=1)), output_field=IntegerField() ), error_count= Sum( Case(When(messages__status__iexact='error', then=1)), output_field=IntegerField() ), sent_yesterday= Sum( Case(When(messages__date_sent__gte=yesterday, then=1)), output_field=IntegerField() ) ) }}} And when viewing SQL query for this annotation, I see every single field in this model is included in GROUP BY clause, making query very slow. There's also one field from related model included in GROUP BY. I've run the query manually, removing all unnecessary fields from the clause and it runs fine" Bug closed Database layer (models, ORM) 1.8 Normal needsinfo orm, sql, annotation Unreviewed 0 0 0 0 0 0