mysql 中位数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lucky_boy_gilr/article/details/51583903

mysql中位数 奇数取中间的值,偶数取中间两个数的平均值

eg: 12345 中位数4 1234 中位数2.5

SELECT avg(t1.money) as median_val FROM (
SELECT @rownum:=@rownum+1 as row_number, d.money
FROM core_order d, (SELECT @rownum:=0) r
WHERE 1
– (条件)
ORDER BY d.money
) as t1,
(
SELECT count(*) as total_rows
FROM core_order d
WHERE 1
– (条件)
) as t2
WHERE 1
AND t1.row_number in ( floor((total_rows+1)/2), floor((total_rows+2)/2) );

展开阅读全文

没有更多推荐了,返回首页