SOQL as a Summary Report

When learning SOQL, I found it helpful to relate things back to something I was familiar with. I'll be using Summary Reports to help explain how to use GROUP BY and GROUP BY ROLLUP in SOQL.

First, a Salesforce Report

Let's start with a Salesforce Report of Accounts grouped by Billing State/Province:


To get this, I created a Report with the Accounts standard report type. Then I created the grouping, added some fields and ran the report.

Writing SOQL

I'll be executing my SOQL via the SFDX Command Line Interface (CLI). If you aren't familiar with this, check out my other post - SOQL from the Command Line

Feel free to use your preferred method to run your SOQL. If you don't have a preferred method, I talk about my favorites in the last section of another post - Why should I use SOQL?

GROUP BY

Here's our example Salesforce Report done via SOQL:


The key difference is that the Salesforce Report gives us data about each record and SOQL just gives us the totals for each grouping. Depending on what you're trying to do, the additional data given by a Salesforce Report may make it the right tool for the job.

In SOQL, we are
  1. SELECT'ing the BillingState field as the field to group on
  2. COUNT'ing the number of Ids returned for each record in the group
  3. Getting data FROM the Account object
  4. GROUP'ing BY the BillingState field
You'll notice that we get a null BillingState in our result. We could have added a WHERE Clause and filtered out null values, but that's a lesson for another day.

GROUP BY ROLLUP

Salesforce Reports have the ability to create nested groupings. For example, if we took our sample report and added Type as a grouping we would see Accounts by Billing State/Province and then by Type:


We can do the same thing in SOQL by using GROUP BY ROLLUP:


In SOQL, we are
  1. SELECT'ing the BillingState and Type fields as the fields to group on
  2. COUNT'ing the number of Ids returned for each record in each group
  3. Getting data FROM the Account object
  4. GROUP'ing BY ROLL'ed UP values of the BillingState and Type fields
I must say, the results here are not super readable at first. The trick is to focus on any fields you set as sub-groups, reading down them until you reach the last null result for the sub-group. This will be the total number of records for the primary grouping.

Let's take CA BillingState as an example. The last null result in the Type column shows a count of 2. This is what we expect because the sample report above has 2 CA Accounts.

The CA BillingState example is an interesting one because it has 2 null results in the Type column. Looking back at our sample report, we can see that the sForce account has a null Type and it is sub-grouped below the CA BillingState. The result of our GROUP BY ROLLUP is reading the null Type and setting it as a sub-group result in the Type column. The Salesforce Report and SOQL are doing the same thing here, they're just displaying them differently.

Continued Learning

I hope you enjoyed this introduction to GROUP BY and GROUP BY ROLLUP in SOQL. If you'd like to go deeper, I recommend checking out these resources for some continued learning:

Comments

  1. I generally check this kind of article and I found your article which is related to my interest. Genuinely it is good and instructive information. Thankful to you for sharing an article like this.import export data provider

    ReplyDelete

Post a Comment

Popular posts from this blog

Joins in SOQL

SOQL from the Command Line

Salesforce Lighting Migration: Plan for Success