Repository: django/django
File: django/db/models/sql/query.py (Python)

Code before:
                raise FieldError(
                    "Using an aggregate in order_by() without also including "
                    "it in annotate() is not allowed: %s" % item
                )
        if errors:
            raise FieldError("Invalid order_by arguments: %s" % errors)
        if ordering:
            self.order_by += ordering
        else:
            self.default_ordering = False

    def clear_ordering(self, force=False, clear_default=True):
        """
        Remove any ordering settings if the current query allows it without
        side effects, set 'force' to True to clear the ordering regardless.
        If 'clear_default' is True, there will be no ordering in the resulting
        query (not even the model's default).
        """
        if not force and (
            self.is_sliced or self.distinct_fields or self.select_for_update
        ):
            return
        self.order_by = ()
        self.extra_order_by = ()
        if clear_default:
            self.default_ordering = False

    def set_group_by(self, allow_aliases=True):
        """
        Expand the GROUP BY clause required by the query.

        This will usually be the set of all non-aggregate fields in the
        return data. If the database backend supports grouping by the
        primary key, and the query would be equivalent, the optimization
        will be made automatically.
        """
        if allow_aliases and self.values_select:
            # If grouping by aliases is allowed assign selected value aliases
            # by moving them to annotations.
            group_by_annotations = {}
            values_select = {}
            for alias, expr in zip(self.values_select, self.select):
                if isinstance(expr, Col):
                    values_select[alias] = expr
                else:
                    group_by_annotations[alias] = expr
            self.annotations = {**group_by_annotations, **self.annotations}
            self.append_annotation_mask(group_by_annotations)
            self.select = tuple(values_select.values())
            self.values_select = tuple(values_select)
            if self.selected is not None:

Review comment (suggestion):
This caused a [regression](https://djangoci.com/job/django-oracle/database=oracle19,label=oracle,python=python3.14/lastCompletedBuild/testReport/) on Oracle but not only. `group_by` is a tuple of `Col` expressions, `order_by` is a list of strings, so `{*self.order_by, *self.extra_order_by}` will never be a subset of `group_by`.

Previously:

```sql
SELECT
   ...
FROM "AGGREGATION_REGRESS_BOOK"
WHERE "AGGREGATION_REGRESS_BOOK"."ID" IN (
    SELECT MAX(U0."ID") AS "ID__MAX"
   FROM "AGGREGATION_REGRESS_BOOK" U0
   GROUP BY U0."CONTACT_ID"
)
ORDER BY "AGGREGATION_REGRESS_BOOK"."ID" ASC
```
with this patch
```sql
SELECT
   ...
FROM "AGGREGATION_REGRESS_BOOK"
WHERE "AGGREGATION_REGRESS_BOOK"."ID" IN (
    SELECT MAX(U0."ID") AS "ID__MAX"
   FROM "AGGREGATION_REGRESS_BOOK" U0
   GROUP BY U0."CONTACT_ID"
   ORDER BY U0."CONTACT_ID" ASC
)
ORDER BY "AGGREGATION_REGRESS_BOOK"."ID" ASC
```

Code after:
                raise FieldError(
                    "Using an aggregate in order_by() without also including "
                    "it in annotate() is not allowed: %s" % item
                )
        if errors:
            raise FieldError("Invalid order_by arguments: %s" % errors)
        if ordering:
            self.order_by += ordering
        else:
            self.default_ordering = False

    def clear_ordering(self, force=False, clear_default=True):
        """
        Remove any ordering settings if the current query allows it without
        side effects, set 'force' to True to clear the ordering regardless.
        If 'clear_default' is True, there will be no ordering in the resulting
        query (not even the model's default).
        """
        if not force and (
            self.is_sliced
            or self.distinct_fields
            or self.select_for_update
            or (
                isinstance(self.group_by, tuple)
                and not {*self.order_by, *self.extra_order_by}.issubset(self.group_by)
            )
        ):
            return
        self.order_by = ()
        self.extra_order_by = ()
        if clear_default:
            self.default_ordering = False

    def set_group_by(self, allow_aliases=True):
        """
        Expand the GROUP BY clause required by the query.

        This will usually be the set of all non-aggregate fields in the
        return data. If the database backend supports grouping by the
        primary key, and the query would be equivalent, the optimization
        will be made automatically.
        """
        if allow_aliases and self.values_select:
            # If grouping by aliases is allowed assign selected value aliases
            # by moving them to annotations.
            group_by_annotations = {}
            values_select = {}
            for alias, expr in zip(self.values_select, self.select):
                if isinstance(expr, Col):
                    values_select[alias] = expr
                else: