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
Kiran Kumar GottulaKiran Kumar Gottula 

Display page bock table on opportunity


Plz help me , I have no idea about aggregate results.


display last two weeks opportunities sum(amount) group by stagename="closed won" and stagename ="closed lost"



week wise   closed wons  closed losts


1st week        2000/-              100/-             ( total opportunities in 1st week )


2nd week       2500/-               250/-            (total opportunities in 2st week)



please post controller

Best Answer chosen by Admin (Salesforce Developers) 
Puneet SardanaPuneet Sardana


You can use the following code,




//query for the last two week
AggergateResult [] aggr=[SELECT SUM(Amount) amount,StageName name

                                              FROM Opportunity

                                              GROUP BY StageName

                                              WHERE CreateDate>:lastToLastWeek AND CreateDate<:lastWeek];


for(AggregateResult agr:aggr)


        String stageName=(String)agr.get('name');

        if(stageName=='closed wons')


                     //get amount for closed wons using agr.get('amount')                  


       else if(stageName=='closedlosts')


                   //get amount for closed losts using agr.get('amount')                  




Use another query to get result for this week.



Please mark as solution if this solves your problem