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:

The RecordGroupBy type is introduced by the RecordQueryAggregate 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 for a complete view of the schema.

The GroupByType type defines the type of Group By clause.

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.

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

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.

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

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.

You can't use group:true for fields of type DateTime and Time. Instead, use these fields with the aggregate functions. See Grouping Examples.

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.

Aggregate Examples

SOQL and SOSL Reference: Date Functions