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
ShravanKumarBagamShravanKumarBagam 

Error on SOQL

Hi,

       I've write a query on opportunity to see avg amount.

How can i see avg amount of opportunity.

 

Error:line 1, column 1: Illegal assignment from LIST to LIST

      

List<Opportunity> lst = [SELECT AVG(Amount) FROM Opportunity];

 

 

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

AggregateResult[] groupedResults  = [SELECT AVG(Amount)aver FROM Opportunity];

system.debug('!!!!!!!'+groupedResults[0].get('aver'));

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

ShravanKumarBagamShravanKumarBagam

Hi Navatar,

  

 

  Could you explain why are we using "AggregateResult[] groupedResults" instead of collection for quering.

Please Provide me reference links

 

  

Jerun JoseJerun Jose

Hey Shravan,

 

You have to remeber that SOQL is not SQL, so the AVG() function in your query does not work the same way as you would in SQL.

 

What Ankit posted was using Aggregate functions in a query. Aggregate functions are used to perform operations like sum, avg, count and others. These follow a specific syntax different from a normal SOQL query. You can read more on them at

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm

 

Thanks,

 

Jerun Jose