just for fun i thought i try and do it all in MySQL, here's the sqlFiddle
SELECT
CASE
WHEN MOD((select count(*) as count from t),2)=1 THEN
(select val from
(select @row:=@row+1 as row,val
from t,(select @row:=0)r
order by val)t1
where t1.row = CEIL((select count(*) as count from t)/2)
)
ELSE
((select val from
(select @row:=@row+1 as row,val
from t,(select @row:=0)r
order by val)t1
where t1.row = (select count(*) as count from t)/2)+
(select val from
(select @row:=@row+1 as row,val
from t,(select @row:=0)r
order by val)t1
where t1.row = ((select count(*) as count from t)/2)+1))/2
END AS median
Just replace occurences of t
with your table name, don't change t1
.
Also if the table has no rows, it'll return NULL as median.
This query can be further reduced to the below (sqlFiddle)
SELECT @rowCount:=(select count(*) as count from t) AS rowcount,
(select AVG(val) from
(select @row:=@row+1 as row,val
from t,(select @row:=0)r
order by val)t1
where t1.row IN (FLOOR((@rowCount+1)/2),
CEIL((@rowCount+1)/2)
)
) as Median
It'll return 2 columns, a rowcount
column and a median
column. I put the rowcount
column there because i didn't want to count from t multiple times like previous query.