                  result.append(sql)
                  params.extend(extra_params)
  
          return result, params
  
      def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
              requested=None, restricted=None):
 
</s> remove                     self.compile(col)
 </s> add                     sql, col_params = self.compile(col) </s> remove                 if sql not in seen:
 </s> add                 if sql not in seen or col_params: </s> remove             for alias, annotation in inner_query.annotation_select.items():
                if annotation.is_summary:
                    # The annotation is already referring the subquery alias, so we
                    # just need to move the annotation to the outer query.
                    outer_query.annotations[alias] = annotation.relabeled_clone(relabels)
 </s> add             col_cnt = 0
            for alias, expression in inner_query.annotation_select.items():
                if expression.is_summary:
                    expression, col_cnt = inner_query.rewrite_cols(expression, col_cnt)
                    outer_query.annotations[alias] = expression.relabeled_clone(relabels)
            # Finally, add needed group by cols from annotations
            for annotation in self.query.annotation_select.values():
                cols = annotation.get_group_by_cols()
                for col in cols:
                    sql = '%s.%s' % (qn(col[0]), qn(col[1]))
                    if sql not in seen:
                        result.append(sql)
                        seen.add(sql)
                  result.append(sql)
                  params.extend(extra_params)
            # Finally, add needed group by cols from annotations
            for annotation in self.query.annotation_select.values():
                cols = annotation.get_group_by_cols()
                for col in cols:
                    sql = '%s.%s' % (qn(col[0]), qn(col[1]))
                    if sql not in seen:
                        result.append(sql)
                        seen.add(sql)
  
          return result, params
  
      def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
              requested=None, restricted=None):
keep add keep keep keep keep keep
 Fixed #23877 -- aggregation's subquery missed target col

Aggregation over subquery produced syntactically incorrect queries in
some cases as Django didn't ensure that source expressions of the
aggregation were present in the subquery. 
django/db/models/sql/compiler.py