So far, we have been using SQL through the Dataverse Web API to answer questions such as:
Which Accounts match a particular condition?
Which Contacts belong to an Account?
Which Opportunities are related to those Accounts?
Those are all record-level questions.
But quite often, the business doesn’t actually want the individual records.
They want answers.
How many active Contacts do we have?
How many Contacts belong to each Account?
What is the total value stored across a set of records?
What is the average value?
This is where aggregation enters the picture.
And if you come from a SQL background, the syntax is going to look very familiar because Dataverse SQL supports:
COUNT()SUM()AVG()MIN()MAX()GROUP BY
But, as we have seen throughout this series, familiar SQL syntax doesn’t necessarily mean unrestricted SQL behaviour.
There are some important Dataverse-specific boundaries we need to understand.
Starting Simple – COUNT the Records
Let’s begin with probably the simplest aggregate question we can ask:
How many Accounts are there?
Using Dataverse SQL:
SELECT COUNT(*) AS account_countFROM account
Rather than returning individual Account rows, this query returns a calculated count.
Conceptually:
account_count-------------2458
This is a very different type of query from:
SELECT nameFROM account
The second query asks Dataverse to return records.
The first asks Dataverse to summarise those records and return the answer.
That distinction becomes increasingly useful when we’re dealing with larger datasets and the consuming application doesn’t actually need every individual row.
COUNT Becomes More Useful with WHERE
Counting every row is useful, but most real business questions contain conditions.
For example:
How many active Contacts do we currently have?
We can combine COUNT with WHERE:
SELECT COUNT(*) AS active_contactsFROM contactWHERE statecode = 0
Now Dataverse first applies our filter and then counts the records that satisfy it.
We can make the condition more specific:
SELECT COUNT(*) AS active_contacts_with_emailFROM contactWHERE statecode = 0 AND emailaddress1 IS NOT NULL
Our question has now become:
How many active Contacts have an email address?
This is where aggregation starts becoming genuinely useful.
We’re not asking our application to retrieve thousands of Contacts and count them afterwards.
We’re asking the query to return the summary we’re interested in.
But What If We Want a Count Per Account?
A single overall count is useful.
But imagine the business requirement is:
Show me how many Contacts each Account has.
Now a single:
COUNT(*)
isn’t enough.
We need to divide the records into groups.
That’s where GROUP BY comes in.
Microsoft provides this example:
SELECT a.name, COUNT(*) AS contact_countFROM account AS aINNER JOIN contact AS c ON a.accountid = c.parentcustomeridGROUP BY a.nameORDER BY a.name
There are several things happening here.
First, we’re joining:
Account ↓Contact
Then we’re grouping the resulting records by:
a.name
Finally, within each Account group, we’re calculating:
COUNT(*)
Conceptually, the result might look something like:
Account Contacts------------------------------------Adventure Works 18Contoso 7Fourth Coffee 23Wide World Importers 12
We have moved from retrieving individual Contacts to answering a business question about the relationship.
JOIN and GROUP BY Together
This example also shows why the topics we covered previously start fitting together.
Earlier, we used:
INNER JOIN
to combine related Dataverse tables.
Now we’re using:
INNER JOIN+GROUP BY+COUNT()
to summarise the relationship.
That progression is important.
Consider the difference between these two queries.
This:
SELECT a.name, c.fullnameFROM account AS aINNER JOIN contact AS c ON a.accountid = c.parentcustomerid
asks:
Which Contacts belong to which Accounts?
Whereas:
SELECT a.name, COUNT(*) AS contact_countFROM account AS aINNER JOIN contact AS c ON a.accountid = c.parentcustomeridGROUP BY a.name
asks:
How many Contacts belong to each Account?
Same relationship.
Completely different question.
That is the value aggregation gives us.
COUNT Is Only the Beginning
Dataverse SQL also supports several familiar aggregate functions:
COUNT()SUM()AVG()MIN()MAX()
Microsoft’s documentation demonstrates multiple aggregate calculations in a single query:
SELECT COUNT(*) AS total_accounts, SUM(revenue) AS total_revenue, AVG(revenue) AS avg_revenue, MIN(revenue) AS min_revenue, MAX(revenue) AS max_revenueFROM account
This single query asks several questions at once.
How many Accounts are represented?
What is their total revenue?
What is the average revenue?
What is the minimum?
What is the maximum?
Instead of returning hundreds or thousands of Account rows, the result provides a summary of the dataset.
Understanding SUM
SUM() calculates the total of a numeric column.
For example:
SELECT SUM(revenue) AS total_revenueFROM account
Conceptually, if our records contained:
Account A 100,000Account B 250,000Account C 150,000
then:
SUM(revenue)
would give us:
500,000
We can also combine aggregation with filtering:
SELECT SUM(revenue) AS active_account_revenueFROM accountWHERE statecode = 0
Now the total is calculated only across Accounts satisfying the WHERE condition.
Finding the Average with AVG
Sometimes the total isn’t what we need.
Perhaps we want to understand the average value.
That’s where AVG() comes in.
SELECT AVG(revenue) AS average_revenueFROM account
Or we could calculate multiple values together:
SELECT COUNT(*) AS account_count, SUM(revenue) AS total_revenue, AVG(revenue) AS average_revenueFROM accountWHERE statecode = 0
Now one query gives us:
Number of active AccountsTotal revenueAverage revenue
That can be useful when an API consumer needs summary information rather than the underlying Dataverse records.
Finding the Minimum and Maximum
Dataverse SQL also supports:
MIN()MAX()
For example:
SELECT MIN(revenue) AS minimum_revenue, MAX(revenue) AS maximum_revenueFROM account
Combined with the other aggregate functions, we can produce a useful summary:
SELECT COUNT(*) AS total_accounts, SUM(revenue) AS total_revenue, AVG(revenue) AS average_revenue, MIN(revenue) AS minimum_revenue, MAX(revenue) AS maximum_revenueFROM accountWHERE statecode = 0
From a readability perspective, this is one of the areas where SQL feels very natural.
A developer can look at the query and immediately understand the information being requested.
Filtering Before Aggregating
There is an important point to understand here.
Dataverse SQL supports:
WHERE
but doesn’t support:
HAVING
In traditional SQL, we might use HAVING to filter grouped results.
That option isn’t available here.
Microsoft specifically states that data should be filtered using WHERE before aggregation.
So our thinking needs to be:
Dataverse rows ↓WHERE ↓Filtered rows ↓GROUP BY ↓Aggregation ↓Result
This becomes an important design consideration when translating an existing SQL query into something supported by the Dataverse Web API.
GROUP BY Has Restrictions Too
There is another limitation worth knowing.
Grouping by functions isn’t supported.
For example, developers familiar with SQL might naturally think about something like:
GROUP BY MONTH(a.createdon)
to group records by month.
That isn’t supported.
The documentation specifically calls out grouping by functions, including extracting date parts such as month, as unsupported.
Again, this comes back to the central theme running through this series:
Dataverse SQL uses familiar SQL syntax, but it isn’t full SQL Server functionality.
We need to design within the supported subset.
The 50,000-Record Limit You Need to Know About
This is probably the most important section of this article if you’re thinking about using aggregation against production Dataverse data.
Aggregate queries are subject to a 50,000-record limit.
The important detail is that this is about the records evaluated by the aggregate query.
If the filter criteria cause the aggregate query to evaluate more than 50,000 records, Dataverse can return:
Number: -2147164125Code: 8004E023Message:AggregateQueryRecordLimit exceeded.Cannot perform this operation.Client error message:The maximum record limit is exceeded.Reduce the number of records.
This isn’t something we want to discover only after deploying an integration to production.
Imagine this query:
SELECT COUNT(*) AS contact_countFROM contact
In a small environment containing 10,000 Contacts, it may work perfectly.
Now imagine the same query against an environment containing hundreds of thousands of records.
We need to start thinking differently.
Filter the Dataset
Microsoft’s guidance is to reduce the number of records evaluated by applying appropriate filters.
For example, instead of aggregating across the entire dataset, the query might target a suitable date range or subset of values.
Conceptually:
SELECT COUNT(*) AS contact_countFROM contactWHERE createdon >= DATEADD(day, -30, GETUTCDATE())
Now the question isn’t:
How many Contacts have ever existed?
It becomes:
How many Contacts were created within this date range?
The exact filter obviously depends on the business requirement, but the architectural lesson is important:
Aggregation queries need to be designed with the size of the Dataverse dataset in mind.
What If I Really Need More Than 50,000 Records?
The documentation suggests splitting the query using appropriate filters, executing it multiple times, and then combining the results.
Those filters might be based on things such as:
Date rangesChoice valuesOther suitable subsets of the data
For example, conceptually:
Query 1 → JanuaryQuery 2 → FebruaryQuery 3 → March...
Then the application consuming the API can combine those results where appropriate.
The exact partitioning strategy depends heavily on the nature of the data and the calculation being performed.
But the important point is:
The 50,000-record aggregate limit needs to be part of the query design, not an afterthought.
A Practical Scenario
Let’s put several of these concepts together.
Suppose we have a business requirement:
We need to know how many active Contacts are associated with each Account.
We already know how to join Account and Contact:
FROM account AS aINNER JOIN contact AS c ON a.accountid = c.parentcustomerid
We can filter to active Contacts:
WHERE c.statecode = 0
Then group by Account:
GROUP BY a.name
And finally count the records:
COUNT(*)
Putting everything together:
SELECT a.name, COUNT(*) AS active_contact_countFROM account AS aINNER JOIN contact AS c ON a.accountid = c.parentcustomeridWHERE c.statecode = 0GROUP BY a.nameORDER BY a.name
Now the SQL tells a fairly complete business story.
Start with Accounts ↓Find related Contacts ↓Keep active Contacts ↓Group them by Account ↓Count them ↓Sort by Account
That is much more than simply retrieving Dataverse rows.
We’re asking Dataverse to answer a question about the data.
Where Aggregation Fits
This is where SQL through the Dataverse Web API becomes interesting from an integration perspective.
Imagine an external application only needs:
Account NameActive Contact Count
Do we necessarily want to retrieve every related Contact and calculate the count inside our application?
Not always.
If the supported Dataverse SQL aggregation can answer the question directly, returning the summary may be much cleaner.
The same thinking applies to:
COUNTSUMAVGMINMAX
The consuming application can ask for the information it actually needs rather than necessarily retrieving all of the underlying rows.
Of course, whether this is the appropriate approach depends on the requirement, dataset size, supported SQL capabilities and the limits we’ve discussed.
Aggregation Changes the Question
This is probably the biggest takeaway.
Until now, most of our queries have asked:
Which records match my requirement?
Aggregation allows us to start asking:
What does my data tell me?
We can move from:
SELECT
to:
SELECT+WHERE+JOIN+GROUP BY+COUNT / SUM / AVG / MIN / MAX
and express increasingly useful business questions through the Dataverse Web API.
But with that power comes another important consideration.
As the queries become more sophisticated and datasets become larger, we need to start thinking about how efficiently those queries execute.
Selecting unnecessary columns, expensive filtering patterns, excessive joins and poor paging strategies can all matter.
So the next step isn’t adding even more SQL syntax.
It’s understanding how to query Dataverse responsibly when these queries move beyond experimentation and into real applications.
