2 个回答
Use aggregrateResult and count.
Not sure if that's correct on the syntax - but it would be close.List<aggregateResult> opptysAR = [SELECT ownerId oid, COUNT(id) opptyCount FROM Opportunity ORDER BY ownerId GROUP BY ownerId];
For (AggregateResult ar :opptysAR) {
system.debug('OwnerId, count= ' + ar.get('oid') + ' ' + ar.get('opptyCount');
}