SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate Queries, Neil Hohmann
CzechDreamin
57 views
59 slides
May 23, 2024
Slide 1 of 59
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
About This Presentation
Already know how to write a basic SOQL query? Great! But what about an *aggregate* SOQL query? You know, the kind that uses aggregate functions like COUNT & MAX along with GROUP BY and HAVING clauses? No? Well, get ready to learn how to slice & dice your org’s data right inside your own de...
Already know how to write a basic SOQL query? Great! But what about an *aggregate* SOQL query? You know, the kind that uses aggregate functions like COUNT & MAX along with GROUP BY and HAVING clauses? No? Well, get ready to learn how to slice & dice your org’s data right inside your own dev console. From finding duplicate records to prototyping summary & matrix reports, learn the ins and outs of aggregate queries during this fast-paced but admin-friendly session on advanced SOQL concepts.
Size: 8.85 MB
Language: en
Added: May 23, 2024
Slides: 59 pages
Slide Content
Neil Hohmann SOQL 201 for Admins & Developers: Slice & Dice Your Org’s Data With Aggregate Queries
SOQL = Hacking Into Your Own Org
SOQL = Summoning Spell
Aggregate Functions GROUP BY GROUP BY ROLLUP GROUPING function Date Functions HAVING SOQL 201: Aggregate Queries
Developer Console > Query Editor
SELECT Id, Name, FORMAT(AnnualRevenue) FROM Account WHERE CreatedDate = THIS_MONTH SELECT COUNT(Id) FROM Account WHERE CreatedDate = THIS_MONTH Standard SOQL vs Aggregate SOQL
Standard SOQL vs Aggregate SOQL
SELECT Donor_Level__c, COUNT( Id ) , FORMAT( SUM( CC_Total_Given_Pledged__c ) ), FORMAT( AVG( CC_Total_Given_Pledged__c ) ) FROM Account WHERE CC_Total_Given_Pledged__c > 0 GROUP BY ROLLUP ( Donor_Level__c ) ORDER BY Donor_Level__c NULLS LAST Aggregate SOQL Example
Aggregate SOQL Example
SELECT Name, COUNT( Id ) FROM Opportunity GROUP BY Name HAVING COUNT(Id) > 1 Aggregate SOQL Example
SELECT StageName, COUNT(Id), SUM(Amount), MIN(Amount), MAX(Amount), AVG(Amount) FROM Opportunity GROUP BY StageName GROUP BY fieldName
GROUP BY fieldName
GROUP BY fieldName
GROUP BY fieldName
GROUP BY fieldName
Write an aggregate SOQL query that breaks down ‘Closed Won’ opportunities by account industry. Include the opportunity record count, as well as the sum and average of the amount for each industry. Be sure to format the currency fields. SOQL CHALLENGE #1 SELECT FROM WHERE GROUP BY
GROUP BY fieldName
GROUP BY ROLLUP
GROUP BY fieldName
SELECT StageName, COUNT(Id), FORMAT(SUM(Amount)), FORMAT(MIN(Amount)), FORMAT(MAX(Amount)), FORMAT(AVG(Amount)) FROM Opportunity GROUP BY ROLLUP ( StageName ) ORDER BY StageName NULLS LAST GROUP BY fieldName
GROUP BY ROLLUP(fieldName)
GROUP BY ROLLUP(fieldName)
GROUPING Function
SELECT StageName, GROUPING( StageName ) total, COUNT(Id), FORMAT(SUM(Amount)), FORMAT(MIN(Amount)), FORMAT(MAX(Amount)), FORMAT(AVG(Amount)) FROM Opportunity GROUP BY ROLLUP ( StageName ) ORDER BY StageName NULLS LAST GROUPING(fieldName)
GROUPING(fieldName)
GROUPING(fieldName)
Date Functions
Date Functions Date functions allow you to group and filter data by date periods (eg, day, calendar month, fiscal quarter, etc.): CALENDAR_MONTH() → returns a number 1 - 12 CALENDAR_YEAR() → returns the year DAY_IN_WEEK () → returns a number 1 - 7 FISCAL_YEAR() → returns the fiscal year FISCAL_QUARTER() → returns the fiscal quarter (1 - 4)
SELECT CALENDAR_YEAR( MC4SF__MC_Campaign__r.MC4SF__Create_Time__c ) , CALENDAR_MONTH( MC4SF__MC_Campaign__r.MC4SF__Create_Time__c ) , COUNT_DISTINCT(MC4SF__MC_Campaign__r.Name), MC4SF__Action__c, COUNT(MC4SF__MC_Subscriber__c), COUNT_DISTINCT(MC4SF__MC_Subscriber__c) FROM MC4SF__MC_Subscriber_Activity__c WHERE MC4SF__Action__c IN ('sent', 'open', 'click') GROUP BY CALENDAR_YEAR( MC4SF__MC_Campaign__r.MC4SF__Create_Time__c ) , CALENDAR_MONTH( MC4SF__MC_Campaign__r.MC4SF__Create_Time__c ) , MC4SF__Action__c ORDER BY CALENDAR_YEAR( MC4SF__MC_Campaign__r.MC4SF__Create_Time__c ) DESC , CALENDAR_MONTH( MC4SF__MC_Campaign__r.MC4SF__Create_Time__c ) DESC , MC4SF__Action__c DESC Date Functions : CALENDAR_YEAR(), CALENDAR_MONTH()
Date Functions: CALENDAR_YEAR(), CALENDAR_MONTH()
Write a query that breaks down ‘Closed Won’ opportunities by account owner, year, and month. Include the record count and the sum. Also, include the most recent opportunity close date. Include a roll-up summary field. Format all currency and date fields. SOQL CHALLENGE #2 SELECT FROM WHERE GROUP BY
SOQL CHALLENGE #2
HAVING
SELECT Account.Name, COUNT(Id) FROM Opportunity WHERE StageName = 'Closed Won' GROUP BY Account.Name HAVING Count(Id) >= 3 HAVING
HAVING
Write a query that catches duplicate contacts. It should only identify duplicates if both the name and email are found in another contact. It should return the name, email, and number of matches. Make sure it doesn’t return records where the email field is null or blank. SOQL CHALLENGE #3 SELECT FROM WHERE GROUP BY HAVING
SOQL CHALLENGE #3
Resources Salesforce SOQL and SOSL Reference (Spring ‘24) neilhohmann.com: SOQL 101 Part 3: Aggregate Queries CampApex.org (SOQL section) Coding with the Force (YouTube): SOQL & SOSL Guide Trailhead: SOQL for Admins > Use Bind Variables and Aggregate Functions