Find a Median in Tables
By Robby
The simplest query to find the Median
SELECT x.val from table x, table y GROUP BY x.val HAVING SUM(SIGN(1-SIGN(y.val-x.val))) = (COUNT(*) +1)/2
The simplest query to find the Median
SELECT x.val from table x, table y GROUP BY x.val HAVING SUM(SIGN(1-SIGN(y.val-x.val))) = (COUNT(*) +1)/2