Calculating the Median with Mysql

后端 未结 7 1743
傲寒
傲寒 2020-12-17 00:38

I\'m having trouble with calculating the median of a list of values, not the average.

I found this article Simple way to calculate median with MySQL

It has a

7条回答
  •  情书的邮戳
    2020-12-17 00:59

    First try to understand what the median is: it is the middle value in the sorted list of values.

    Once you understand that, the approach is two steps:

    1. sort the values in either order
    2. pick the middle value (if not an odd number of values, pick the average of the two middle values)

    Example:

    Median of 0 1 3 7 9 10: 5 (because (7+3)/2=5)
    Median of 0 1 3 7 9 10 11: 7 (because 7 is the middle value)
    

    So, to sort dates you need a numerical value; you can get their time stamp (as seconds elapsed from epoch) and use the definition of median.

提交回复
热议问题