Calculate a median with MySQL

Today I read this article and found this technique for calculating a median value:

SELECT x.val from data x, data y
GROUP BY x.val
HAVING SUM(SIGN(1-SIGN(y.val-x.val)))/COUNT(*) > .5
LIMIT 1

Leave a Reply