ApexSensei

Drills SOQL queries

Keep busy Account groups

GROUP BY returns one row per value.

Your task

Group this run's Opportunities by Account and keep only Accounts with two or more.

String tag = 'AS-Q311-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
String stageName = Opportunity.StageName.getDescribe().getPicklistValues()[0].getValue();
List<Account> parents = new List<Account>{
    new Account(Name = tag + '-Three'),
    new Account(Name = tag + '-Two'),
    new Account(Name = tag + '-One')
};
insert parents;
insert new List<Opportunity>{
    new Opportunity(Name = tag + '-A1', AccountId = parents[0].Id,
        StageName = stageName, CloseDate = Date.today()),
    new Opportunity(Name = tag + '-A2', AccountId = parents[0].Id,
        StageName = stageName, CloseDate = Date.today()),
    new Opportunity(Name = tag + '-A3', AccountId = parents[0].Id,
        StageName = stageName, CloseDate = Date.today()),
    new Opportunity(Name = tag + '-B1', AccountId = parents[1].Id,
        StageName = stageName, CloseDate = Date.today()),
    new Opportunity(Name = tag + '-B2', AccountId = parents[1].Id,
        StageName = stageName, CloseDate = Date.today()),
    new Opportunity(Name = tag + '-C1', AccountId = parents[2].Id,
        StageName = stageName, CloseDate = Date.today())
};

Requirements

  1. Group count should be computed from the query
  2. GROUP BY AccountId should be present
  3. HAVING COUNT(Id) >= 2 should be present
  4. Groups kept: should be 2
  5. Groups high: should be true
  6. Groups exactly two: should be true
  7. Groups below excluded: should be true

Sign in to open this drill