Created
March 21, 2017 00:38
-
-
Save msrivastav13/d23c499e3fa07ccc6ac45d4aa24e40cb to your computer and use it in GitHub Desktop.
Simple apex Utility to calculate Standard Deviation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class StandardDeviationComputation { | |
public static decimal getStandardDeviation(Integer precision){ | |
AggregateResult[] groupedResults = [SELECT COUNT(Id),SUM(DistanceSquare__c) FROM Survey__c GROUP BY Standard_Deviation__c ]; | |
decimal sumOfDistanceSquare = (decimal)groupedResults[0].get('expr1'); | |
integer count = (integer)groupedResults[0].get('expr0'); | |
if(count > 0){ | |
return system.Math.sqrt(sumOfDistanceSquare/count).setScale(precision);//Scale is kept at 3 currently | |
}else{ | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment