Simple way to calculate median with MySQL

后端 未结 30 1573
北荒
北荒 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:58

    I have this below code which I found on HackerRank and it is pretty simple and works in each and every case.

    SELECT M.MEDIAN_COL FROM MEDIAN_TABLE M WHERE  
      (SELECT COUNT(MEDIAN_COL) FROM MEDIAN_TABLE WHERE MEDIAN_COL < M.MEDIAN_COL ) = 
      (SELECT COUNT(MEDIAN_COL) FROM MEDIAN_TABLE WHERE MEDIAN_COL > M.MEDIAN_COL );
    

提交回复
热议问题