Skip to main content

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

Popular posts from this blog

Joins in SOQL

If you are familiar with SQL, the biggest gotcha when writing SOQL is how joins are done. You'll see the term  Sub-Query  used instead of  JOIN  because of how queries are done on the Salesforce platform. You can still do joins; it's just done a little differently than you're used to. Sub-Query (Outer Join) In SQL, a join is when rows are selected from multiple tables and joined together on common columns. Thinking about your tables like a Venn Diagram is a great analogy for this. Let's say you want to query all Accounts and also include their related Opportunities. This is an example of an Outer Join because it selects all Accounts and will include their related Opportunities via the sub-query. If an Account does not have an Opportunity, it will still be returned here. An Outer Join is similar to selecting one whole circle in a Venn Diagram, including the area that overlaps with the other circle. Sub-Query (Inner Join) An inner join can seem a little m

Salesforce Lighting Migration: Plan for Success

In April 2018, I led the Salesforce Lightning Migration at Postmates . This is how I would do it over again if I could. Define Goals The goals of a Lightning Migration are: Enable Lightning Experience Configure Lightning Experience Document all Salesforce processes Create training and support materials Lightning Experience is adopted Easy enough right? Let's go though the plan to see how we accomplish each one. Enable Lightning Experience Just turn it on right? Well, kinda. Although it may be controversial, I recommend turning it on and hiding the option from all users. This can be done by removing the Lightning Experience User permission from your custom profiles.  And yes, turn it on in production .  Why? You're going to turn it on anyway. Salesforce has declared this as the path forward, so you either turn it on now or wait until they flip the switch for you. Plus, turning this on ahead of time will make deploying from a sandbox much  e

Modify Salesforce Data from the Command Line

When doing Salesforce data loads, how often are you putting the same value in an entire column? I find this happening when I need to: Move all accounts from one user to another Assign a set of leads to a specific user Set a field to a specific value Let's use the example from my other post: Salesforce Data Loads from the Command Line - Move all accounts owned by one user to another user. Magic Let's break down each piece of this: soql This is an alias I set for sfdx force:data:soql:query --query . It saves me from having to type this out each time and let's me query from the command line by just typing soql  and my query. This is a popular StackExchange post  that talks about how to set an alias. cat This command outputs the contents of a file. It is common to use this command with other commands by piping the output into another command. In our example, we are piping the output into the cut command. This post gives more examples of how to use t