function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
baller4life7baller4life7 

SOQL - Leads grouped by Owner and Status

Hey guys,

I want to get data for a table like this:

 

Owner Name   |   # Leads Status 1  |   # Leads Status 2  |   # Leads Status 3  |   # Leads Status 4  

ABC                                      1                                  3                                    1                                  4

DEF                                      3                                  2                                    4                                  2

 

Can I get the required Information in 1 SOQL statement?

 

Thanks,

Josh

CLKCLK

Yes you can get it

[select owner,status from lead group by owner]

baller4life7baller4life7

CLK, thank you for the hint!

My final statement is:

 

select owner.name , status , count(id) from lead group by owner.name, status order by owner.name ASC, status ASC

 

baller4life7baller4life7

Well, I was a little too fast. It's not the solution, I'm looking for...

My SOQL result should look like this:

 

Name   |   Status   |   Count

N1                S1              0

N1                S2              5

N1                S3              3

N2                S1              1

N2                S2              0

N2                S3              2

 

How can I realize that?

Thanks!



CLKCLK

sample code :

 

AggregateResult[] groupedResults
= [SELECT CampaignId, AVG(Amount)
FROM Opportunity
GROUP BY CampaignId];
for (AggregateResult ar : groupedResults) {
System.debug('Campaign ID' + ar.get('CampaignId'));
System.debug('Average amount' + ar.get('expr0'));
}