Search This Blog

Friday 17 January 2014

Oracle SQL: NORMDIST Function for creating Bell Curve Charts

I had been scouring the web for a solution to find some sort of function to produce the same result as the NORMDIST Excel produces with no luck.
Therefore after decoding Microsoft's miniature equation in their help file,



I have created the below function. 

create or replace FUNCTION "NORMDIST"(
      x_VAL IN number,
      x_Mean IN number,
      x_STDEV IN number
      )
    RETURN number
  IS
    NDIST number;
  BEGIN
    
    NDIST := round((1/(sqrt(asin(1)*4)*X_STDEV))*exp(-((power((X_VAL-X_MEAN),2))/(2*power(X_STDEV,2)))),14);
    RETURN (NDIST);
    
  EXCEPTION
  WHEN OTHERS THEN
    RETURN NULL;
  END;


The function should produce the same outcome as "=NORMDIST(Val, Mean, Stdev, False)" in Excel. I don't have a degree in maths, but after testing the code it works and displays the same results. Hope it helps.