Implementing Geometric Mean in MySQL

3:06 pm Math, Programming, SQL, Statistics

This 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.

2 Responses

  1. Implementing Harmonic Mean in MySQL | Full of BS Says:

    [...] 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. [...]

  2. linde Says:

    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!

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.