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
pooja biswaspooja biswas 

aggregate result issue

Hi
I need the average amount and sum of amount from opportunity grouped by stageName.
I have given the code, but the page is displaying same values for average and sum of amount.

Am I missing anything?
global class AggregateExample
{
    //3. 
    public List<AggregateResult> aggrList=new List<AggregateResult>();
    
    //4.
    public AggregateExample()
    {
       aggrList=[select StageName,AVG(Amount),SUM(Amount) from opportunity Group By StageName limit 50000];
    }
       
    //5. create list object of wrapper class
    List<oppor_wrapper> opr=new List<oppor_wrapper>();
    
    //6.
    global List<oppor_wrapper> getResultList()
    {
       for(AggregateResult ar:aggrList)
       {
         oppor_wrapper o=new oppor_wrapper(ar);
         
         opr.add(o);
       }
        return opr;
    }

    global class oppor_wrapper
    {
       //1.
       public String StageName{get;set;}
       
       public Decimal Amount{get;set;}
       
       //2.
       public oppor_wrapper(AggregateResult aggr)
       {
         this.StageName=(String)aggr.get('StageName');
         this.Amount=(Double)aggr.get('expr0');
         this.Amount=(Decimal)aggr.get('expr1');
        
       }
    }
}
<apex:page controller="AggregateExample"
                    contentType="vnd.ms-excel#reporting.xls>
           
 <apex:pageBlock title="Result">
        <apex:pageBlockTable value="{!resultList}" 
                             var="o">
                
            <apex:column value="{!o.stageName}" 
                         headerValue="StageName" />
                         
            <apex:column value="{!o.Amount}" 
                         headerValue="AverageAmount"/>
                         
            <apex:column value="{!o.Amount}" 
                         headerValue="SumAmount"/>
                                   
                         
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Thanks
pooja
 
Best Answer chosen by pooja biswas
Ray C. KaoRay C. Kao
Please have fun with it

Visualforce Page
<apex:page controller="AggregateExample" contentType="vnd.ms-excel#reporting.xls">
  <apex:pageBlock title="Result">
    <apex:pageBlockTable value="{!resultList}" var="o">
      <apex:column value="{!o.stageName}" headerValue="StageName" />
      <apex:column value="{!o.AmountAvg}" headerValue="AverageAmount"/>
      <apex:column value="{!o.AmountSum}" headerValue="SumAmount"/>
    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

Apex Class
global class AggregateExample
{
    //3. 
    public List<AggregateResult> aggrList=new List<AggregateResult>();
    
    //4.
    public AggregateExample()
    {
       aggrList=[select StageName,AVG(Amount),SUM(Amount) from opportunity Group By StageName limit 50000];
    }
       
    //5. create list object of wrapper class
    List<oppor_wrapper> opr=new List<oppor_wrapper>();
    
    //6.
    global List<oppor_wrapper> getResultList()
    {
       for(AggregateResult ar:aggrList)
       {
         oppor_wrapper o=new oppor_wrapper(ar);
         
         opr.add(o);
       }
        return opr;
    }

    global class oppor_wrapper
    {
       //1.
       public String StageName{get;set;}
       
       public Decimal AmountAvg{get;set;}
       public Decimal AmountSum{get;set;}
       
       //2.
       public oppor_wrapper(AggregateResult aggr)
       {
         this.StageName=(String)aggr.get('StageName');
         this.AmountAvg=(Double)aggr.get('expr0');
         this.AmountSum=(Decimal)aggr.get('expr1');
        
       }
    }
}

 

All Answers

Ray C. KaoRay C. Kao
Please have fun with it

Visualforce Page
<apex:page controller="AggregateExample" contentType="vnd.ms-excel#reporting.xls">
  <apex:pageBlock title="Result">
    <apex:pageBlockTable value="{!resultList}" var="o">
      <apex:column value="{!o.stageName}" headerValue="StageName" />
      <apex:column value="{!o.AmountAvg}" headerValue="AverageAmount"/>
      <apex:column value="{!o.AmountSum}" headerValue="SumAmount"/>
    </apex:pageBlockTable>
  </apex:pageBlock>
</apex:page>

Apex Class
global class AggregateExample
{
    //3. 
    public List<AggregateResult> aggrList=new List<AggregateResult>();
    
    //4.
    public AggregateExample()
    {
       aggrList=[select StageName,AVG(Amount),SUM(Amount) from opportunity Group By StageName limit 50000];
    }
       
    //5. create list object of wrapper class
    List<oppor_wrapper> opr=new List<oppor_wrapper>();
    
    //6.
    global List<oppor_wrapper> getResultList()
    {
       for(AggregateResult ar:aggrList)
       {
         oppor_wrapper o=new oppor_wrapper(ar);
         
         opr.add(o);
       }
        return opr;
    }

    global class oppor_wrapper
    {
       //1.
       public String StageName{get;set;}
       
       public Decimal AmountAvg{get;set;}
       public Decimal AmountSum{get;set;}
       
       //2.
       public oppor_wrapper(AggregateResult aggr)
       {
         this.StageName=(String)aggr.get('StageName');
         this.AmountAvg=(Double)aggr.get('expr0');
         this.AmountSum=(Decimal)aggr.get('expr1');
        
       }
    }
}

 
This was selected as the best answer
pooja biswaspooja biswas
Hi
thanks, I overlooked this issue.