Implementing Geometric Mean in MySQL
July 4, 2008 Math, Programming, SQL, Statistics 2 CommentsThis turns out to be pretty easy. You might also want to read Implementing Harmonic Mean in MySQL.
Why bother blogging something this simple? Because I couldn’t find one pre-made, and that means it’s time to bring my mighty google rank of like negative two to bear to fix the problem.
CREATE TABLE example(val integer); INSERT INTO example VALUES(1),(2),(4),(8),(16); SELECT exp(avg(ln(val))) as gmean from example;
That’s it. You should see output like this:
mysql> CREATE TABLE example(val integer); Query OK, 0 rows affected (0.09 sec) mysql> INSERT INTO example VALUES(1),(2),(4),(8),(16); Query OK, 5 rows affected (0.01 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> SELECT exp(avg(ln(val))) as gmean from example; +- - - -+ | gmean | +- - - -+ | 4 | +- - - -+ 1 row in set (0.00 sec)
Still haven’t figured out how to do central moments, skewness or kurtosis.
