Simple way to calculate median with MySQL

后端 未结 30 1569
北荒
北荒 2020-11-22 04:20

What\'s the simplest (and hopefully not too slow) way to calculate the median with MySQL? I\'ve used AVG(x) for finding the mean, but I\'m having a hard time fi

30条回答
  •  耶瑟儿~
    2020-11-22 04:54

    A simple way to calculate Median in MySQL

    set @ct := (select count(1) from station);
    set @row := 0;
    
    select avg(a.val) as median from 
    (select * from  table order by val) a
    where (select @row := @row + 1)
    between @ct/2.0 and @ct/2.0 +1;
    

提交回复
热议问题