SOQL GROUP BY: Tasks by Status
Aggregate SOQL that breaks tasks down by Status, with COUNT, SUM, and the AggregateResult alias rules.
Updated 2026-09-09
Aggregate SOQL gives you a report-style breakdown without building a report: totals per Status, returned as AggregateResult rows.
Activity data is stored differently from other objects, so tasks reward tight filters and rarely need every field selected.
The query
total is an alias. Without it, the column comes back as expr0, which is a common source of confusion when you consume the result in Apex.
SELECT Status, COUNT(Id) total
FROM Task
GROUP BY Status
ORDER BY COUNT(Id) DESCAdd a time window
SELECT Status, COUNT(Id) total
FROM Task
WHERE CreatedDate = THIS_QUARTER
GROUP BY Status
ORDER BY COUNT(Id) DESCAggregate functions available
COUNT(field)/COUNT(Id)- number of rows in the groupCOUNT_DISTINCT(field)- unique valuesSUM(field),AVG(field),MIN(field),MAX(field)on numeric and date fieldsGROUP BY ROLLUP(field)to add a grand-total row
Common mistakes to avoid
- Use the API name
Task, not the UI label - the two differ on several standard objects. - Wrap literal text values in single quotes; date literals such as
LAST_N_DAYS:30must stay unquoted. - Run the query in a sandbox first if it feeds a bulk update - a
WHEREclause that is one character off can select the whole table. - Every non-aggregate field in the SELECT list must appear in
GROUP BY. - Aggregate queries return
AggregateResultobjects, not sObjects - you cannot dot-walk into them the same way.
Run it faster with TurboKit
Aggregates are the fastest way to sanity-check a data model before you trust a dashboard built on it.
TurboKit's AI SOQL builder lets you describe the result you want in plain English, returns the query, and exports the rows to CSV or Excel in the same panel - without leaving the Salesforce tab you are already on.
Frequently asked questions
- What is AggregateResult?
- It is the generic result type returned by any grouped or aggregate SOQL query. Fields are read by alias, and unaliased aggregate columns are named
expr0,expr1, and so on. - Is there a row limit on GROUP BY queries?
- Yes - grouped queries return at most 2,000 groups by default in Apex contexts. Filter more tightly, or use a report or bulk export if you need every group.
- Can I group Task by a formula field?
- Only if the formula is deterministic and groupable. Text formulas usually work; formulas referencing other objects often do not, and the query returns an error rather than a partial result.
More from the SOQL Library
- SOQL Query to Count Tasks in SalesforceCopy-paste SOQL to count tasks in Salesforce, with variants for filtered counts, COUNT(Id) vs COUNT(), and grouped totals.
- SOQL GROUP BY: Accounts by IndustryAggregate SOQL that breaks accounts down by Industry, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Campaigns by TypeAggregate SOQL that breaks campaigns down by Type, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Cases by StatusAggregate SOQL that breaks cases down by Status, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Contacts by LeadSourceAggregate SOQL that breaks contacts down by LeadSource, with COUNT, SUM, and the AggregateResult alias rules.
- SOQL GROUP BY: Leads by LeadSourceAggregate SOQL that breaks leads down by LeadSource, with COUNT, SUM, and the AggregateResult alias rules.
Related reading
- Admin How-ToHow to Write Salesforce Queries Without Knowing SOQL (AI SOQL)Describe the records you want in plain English and get a runnable SOQL query back - how AI SOQL builders work, what to check before you run the result, and where they still fall short.
- GlossaryWhat Is an AI SOQL Builder in Salesforce?An AI SOQL builder turns a plain-English description of the records you want into a runnable SOQL query, using the schema of the org you are working in so the