apex – Aggregate results along with respective record names

How to get the MAX(Amount), Min(Amount) from a list of opportunity records with respective Opportunity Names in a single SOQL query.

for(AggregateResult agResult : [Select Name, MIN(Amount) 'MinAmount', MAX(Amount) 'MaxAmount' 
                                 from Opportunity group by Name]){

     Decimal opMinAmount = (Decimal)agResult.get('MinAmount');
     Decimal opMaxAmount = (Decimal)agResult.get('MaxAmount');   
     String maxAmountOpName = agResult.get(Name);
     //How to get the Minimum Amount Opportunity Name.
} 

Read more here: Source link