【转】MySQL如何计算中位数
http://blog.sina.com.cn/jjbenben
前2天,老板有个报表需求,需要用到中位数算法。而MYSQL没有直接提供median()这样的直接算中位数的算法。于是就在百度上百度了下。看到有为朋友提供了一种算法。但是只要代码,没有解释。花了挺久的时间,终于理解了这种算法的含义。
as
select user_id,avg(price)
from (
select e.user_id, e.price
from
producte e, producte d
where e.user_id = d.user_id
group by e.user_id, e.price
having sum(case when e.price = d.price then 1 else 0 end)>=
abs(sum(sign(e.price - d.price)))
)t
group by user_id。
from producte e, producte d
where e.user_id = d.user_id
group by e.user_id, e.price
having sum(case when e.price = d.price then 1 else 0 end)>=
abs(sum(sign(e.price - d.price)))

浙公网安备 33010602011771号