You need to sign in to do that
Don't have an account?
Query three records, and perform calculations on them
I'm new to apex... I'm trying to query the most three recent records based on a date field and then get the average.. I figure it would look something like this:
SELECT Amount__c FROM cObj__c ORDER BY Date__c ASC LIMIT 3
I'm not sure what happens next. What I think I need to do is store each value in a separate variable or store the sum of the query result in a variable so I can get the average.
If I'm using the right approach here, I want to insert the final calculated value into a field, similar to the "Hello World" Apex example, except "World" is a dynamic value.
Thanks for your help, even if it is just to say this is the completely wrong way.
You can do the average calc in the query, something like
Thanks Simon..
I tried a few things, but couldn't get it done. When using the aggregate function AVG() I can't seem to use ORDER BY, which is critical.
Could I do something like this:
And then do what I need to do with the three amount values... in this case, I want to insert the average of the three Amount__c values into a field (using a trigger).. I just don't know how to reference the retrieved values:
Does that make any sense? I've got what I want and I know what do with them, I just don't know how. Thanks for your help!
Greg