Implementing Geometric Mean in MySQL
July 4, 2008 3:06 pm Math, Programming, SQL, StatisticsThis 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.

July 4th, 2008 at 3:08 pm
[...] Implementing Harmonic Mean in MySQL July 4, 2008 3:02 pm admin Math, Programming, SQL, Statistics This turns out to be pretty easy. You might also want to read Implementing Geometric Mean in MySQL. [...]
June 29th, 2009 at 7:02 pm
hey, thanks for this. i did in fact find this via google, it’s now result #2 for the search “geometric mean mysql”. the post is very appreciated!