              relabels = dict((t, 'subquery') for t in inner_query.tables)
              relabels[None] = 'subquery'
              # Remove any aggregates marked for reduction from the subquery
              # and move them to the outer AggregateQuery.
              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)
                      del inner_query.annotation_select[alias]
              try:
                  outer_query.add_subquery(inner_query, using)
              except EmptyResultSet:
                  return dict(
 
</s> remove                 if sql not in seen:
 </s> add                 if sql not in seen or col_params: </s> remove                     self.compile(col)
 </s> add                     sql, col_params = self.compile(col)
            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)
              relabels = dict((t, 'subquery') for t in inner_query.tables)
              relabels[None] = 'subquery'
              # Remove any aggregates marked for reduction from the subquery
              # and move them to the outer AggregateQuery.
            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)
            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)
            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)
            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)
            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)
                      del inner_query.annotation_select[alias]
              try:
                  outer_query.add_subquery(inner_query, using)
              except EmptyResultSet:
                  return dict(
keep keep keep keep replace replace replace replace replace 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/query.py