Sub topic 11
Grouping by calendar and fiscal date parts
This chapter assumes you can write a basic GROUP BY query and read aggregate results with aliases. You will group records by parts of a date, such as the year, quarter, or month, instead of by the full date value.
Why group by date parts
A date field has a different value for almost every record, so GROUP BY CloseDate returns nearly one row per record. You usually want a coarser bucket, such as total sales per year. SOQL provides date functions that extract one part of a date. You can use them in SELECT, GROUP BY, and ORDER BY.
Calendar and fiscal functions
Calendar functions follow the Gregorian calendar. Fiscal functions follow your org's fiscal year settings, so they can differ from calendar values.
| Calendar function | Fiscal function | Returns |
|---|---|---|
| `CALENDAR_YEAR()` | `FISCAL_YEAR()` | The year |
| `CALENDAR_QUARTER()` | `FISCAL_QUARTER()` | Quarter, 1 to 4 |
| `CALENDAR_MONTH()` | `FISCAL_MONTH()` | Month, 1 to 12 |
Other functions include DAY_IN_MONTH(), DAY_IN_WEEK(), WEEK_IN_YEAR(), and HOUR_IN_DAY().
Group by one date part
This query totals won opportunity amounts per calendar year.
SELECT CALENDAR_YEAR(CloseDate) yr, SUM(Amount) total
FROM Opportunity
WHERE IsWon = true
GROUP BY CALENDAR_YEAR(CloseDate)
ORDER BY CALENDAR_YEAR(CloseDate)The expression in GROUP BY must match the expression in SELECT. Ordering by the same expression sorts the years in ascending order.
Group by several date parts
CALENDAR_MONTH() returns only 1 to 12. If your data spans several years, grouping by month alone merges every January together. Group by year and month to keep them apart.
SELECT CALENDAR_YEAR(CloseDate) yr,
CALENDAR_MONTH(CloseDate) mo,
COUNT(Id) cnt
FROM Opportunity
WHERE IsWon = true
GROUP BY CALENDAR_YEAR(CloseDate), CALENDAR_MONTH(CloseDate)
ORDER BY CALENDAR_YEAR(CloseDate), CALENDAR_MONTH(CloseDate)Use fiscal functions
If your fiscal year starts in April, a deal closed in May is in calendar year 2026 and fiscal quarter 1. The fiscal functions use the org's setting, so you do not calculate the shift yourself.
SELECT FISCAL_YEAR(CloseDate) fy,
FISCAL_QUARTER(CloseDate) fq,
SUM(Amount) total
FROM Opportunity
WHERE IsWon = true
GROUP BY FISCAL_YEAR(CloseDate), FISCAL_QUARTER(CloseDate)
ORDER BY FISCAL_YEAR(CloseDate), FISCAL_QUARTER(CloseDate)Read the results in Apex
Each function result is an Integer value in the AggregateResult, but get returns Object. Cast the value after you read it by alias.
for (AggregateResult ar : [
SELECT FISCAL_YEAR(CloseDate) fy, SUM(Amount) total
FROM Opportunity
WHERE IsWon = true
GROUP BY FISCAL_YEAR(CloseDate)
]) {
Integer fy = (Integer) ar.get('fy');
Decimal total = (Decimal) ar.get('total');
System.debug('FY' + fy + ': ' + total);
}Time zones with DateTime fields
Date functions applied to a Date field, such as CloseDate, have no time zone issue. A DateTime field, such as CreatedDate, is stored in UTC, and date functions evaluate it in the running user's time zone. Records near midnight can land in a different bucket than you expect. Wrap the field in convertTimezone() to make the conversion explicit.
SELECT CALENDAR_MONTH(convertTimezone(CreatedDate)) mo, COUNT(Id) cnt
FROM Lead
GROUP BY CALENDAR_MONTH(convertTimezone(CreatedDate))Common mistakes
- Selecting
CALENDAR_MONTH(CloseDate)but grouping byCloseDate. Group by the same expression you select. - Grouping by month without the year, which merges months from different years.
- Forgetting an alias. Without one, Apex reads the result as
expr0,expr1, and so on.
Exercise
Write a query that counts opportunities per fiscal year and fiscal quarter, ordered chronologically. Why must both functions appear in GROUP BY?
Show answer
SELECT FISCALYEAR(CloseDate) fy, FISCALQUARTER(CloseDate) fq, COUNT(Id) cnt FROM Opportunity GROUP BY FISCALYEAR(CloseDate), FISCALQUARTER(CloseDate) ORDER BY FISCALYEAR(CloseDate), FISCALQUARTER(CloseDate). Every selected non-aggregate expression must be grouped. Grouping by quarter alone would merge the same quarter from different years.