# Group By Schema

| AVAILABLE API VERSION |
| --------------------- |
| API v58.0 and later   |

All sObjects in the schema have a corresponding `RecordGroupBy` type. The `RecordGroupBy` type combines the object API name with the `_GroupBy` suffix. For example:

```sfdocs-code {"lang":"graphql", "title": "RecordQueryAggregate for Account", "src": "" }
type Account {
  groupBy: Account_GroupBy
  orderBy: Account_OrderBy
}
```

The `RecordGroupBy` type is introduced by the [`RecordQueryAggregate` type](/docs/platform/graphql/guide/aggregate.md#recordqueryaggregate-type).

## RecordGroupBy Type

`RecordGroupBy` creates one input type per sObject type. For example, Account has a corresponding `Account_GroupBy` type that you can use with the `groupBy` argument.

The `Account_GroupBy` type contains fields of `GroupByClause`. Some fields are shown here for brevity. We recommend that you use the [Altair GraphQL client](/docs/platform/graphql/guide/get-started-graphql.md) for a complete view of the schema.

```sfdocs-code {"lang":"graphql", "title": "RecordGroupBy Type", "src": "" }
input Account_GroupBy {
  AccountNumber: GroupByClause
  BillingCity: GroupByClause
  BillingCountry: GroupByClause
  CreatedBy: User_GroupBy
  CreatedById: GroupByClause
  Id: GroupByClause
  Industry: GroupByClause
  Name: GroupByClause
  NumberOfEmployees: GroupByClause
  OperatingHours: OperatingHours_GroupBy
  Owner: User_GroupBy
  OwnerId: GroupByClause
  Ownership: GroupByClause
  Parent: Account_GroupBy
  ParentId: GroupByClause
  # More fields here
  type: GroupByType
}
```

## GroupByType Type

The `GroupByType` type defines the type of `Group By` clause.

```sfdocs-code {"lang":"graphql", "title": "GroupByType", "src": "" }
type GroupByType {
  GROUP_BY: GROUP_BY
  ROLLUP: ROLLUP
  CUBE: CUBE
}
```

`GroupByType` contains these fields.

- `GROUP_BY` - The default grouping.
- `ROLLUP` - Adds subtotals for aggregated data in the query result.
- `CUBE` - Adds subtotals for all combinations of a grouped field in the query results.

## GroupByClause Type

The `GroupByClause` type has a `group` field of type `Boolean`. Pass these fields to the `groupBy` argument in the `aggregate` field.

```sfdocs-code {"lang":"graphql", "title": "GroupByClause Type", "src": "" }
type GroupByClause {
  group: Boolean
}
```

The `GroupByClause` type contains the following field.

- `group` - Pass in `true` to the `groupBy` argument to group the query by the specific field. If true, the field is included in the `Group By` SOQL clause.

## User_GroupBy Type

Some fields like `CreatedBy` and `OperatingHours` correspond to a `_GroupBy` type, which contains additional fields of type `OrderByClause` or `_GroupBy`.

```sfdocs-code {"lang":"graphql", "title": "User_GroupBy Type", "src": "" }
type User_GroupBy {
  AboutMe: OrderByClause
  Account: Account_OrderBy
  AccountId: OrderByClause
  # Other User_GroupBy fields
}
```

## GroupByFunction Enum

Use the `GroupByFunction` enumeration with fields of type DateTime and Time. Fields of type DateTime and Time cannot be included with their field name alone, and so providing `group: true` for those field types is invalid.

:::note
You can't use `group:true` for fields of type DateTime and Time. Instead, use these fields with the aggregate functions. See [Grouping Examples](/docs/platform/graphql/guide/aggregate-groupby-examples.md).
:::

```sfdocs-code {"lang":"graphql", "title": "GroupByFunction Enum", "src": "" }
Enum GroupByFunction {
  CALENDAR_MONTH
  CALENDAR_QUARTER
  CALENDAR_YEAR
  DAY_IN_WEEK
  DAY_IN_MONTH
  DAY_IN_YEAR
  DAY_ONLY
  FISCAL_MONTH
  FISCAL_QUARTER
  FISCAL_YEAR
  HOUR_IN_DAY
  WEEK_IN_MONTH
  WEEK_IN_YEAR
}
```

The `GroupByFunction` enumeration contains these values.

- `CALENDAR_MONTH`- Returns a number 1 (January) to 12 (December) for the calendar month on a date field.
- `CALENDAR_QUARTER` - Returns a number 1 (January 1 - March 31) to 4 (October 1 through December 31) for the calendar quarter on a date field.
- `CALENDAR_YEAR` - Returns the calendar year on a date field.
- `DAY_IN_WEEK` - Returns a number 1 (Sunday) to 7 (Saturday) for the day of the week on a date field.
- `DAY_IN_MONTH` - Returns a number 1–31 for the day in the month on a date field.
- `DAY_IN_YEAR` - Returns a number 1–365 for the day in the year on a date field.
- `DAY_ONLY` - Returns a date for the day portion on a DateTime field, for example, `2009-09-22` for September 22, 2009.
- `FISCAL_MONTH` - Returns a number 1–12 for the discal month on a date field, which can differ from `CALENDAR_MONTH` if your organization uses a fiscal year that does not match the Gregorian calendar. This field is not supported if your organization has custom fiscal years enabled.
- `FISCAL_QUARTER` - Returns a number 1–4 for the fiscal quarter on a date field, which can differ from `CALENDAR_QUARTER` if your organization uses a fiscal year that does not match the Gregorian calendar. This field is not supported if your organization has custom fiscal years enabled.
- `FISCAL_YEAR` - Returns the fiscal year on a date field, which can differ from `CALENDAR_YEAR` if your organization uses a fiscal year that does not match the Gregorian calendar.
- `HOUR_IN_DAY` - Returns a number 1–24 for the hour in the day on a DateTime field. Only DateTime and Time fields are supported.
- `WEEK_IN_MONTH` - Returns a number 1–5 for the week in the month on a date field.
- `WEEK_IN_YEAR` - Returns a number 1–53 for the week in the year on a date field.

## See Also

[Aggregate Examples](/docs/platform/graphql/guide/aggregate-examples.md)

[SOQL and SOSL Reference: Date Functions](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_date_functions.htm)
