Skip to main content
 首页 » 数据库

mysql 求中位数

2022年07月18日155lori

实例1:

SET @ID = 0;
SELECT
AVG(loan_amount)
from
(
SELECT @ID:=@ID+1 as ID, loan_amount
FROM   table_xxx ORDER BY loan_amount
) a where IF( ROUND(@ID/2,0)%2=0,ID in (ROUND(@ID/2,0),ROUND(@ID/2,0)/2 + 1), ID=ROUND(@ID/2,0) )

实例2:

SET @ID = 0;
SELECT
AVG(totallimit)
from
(
SELECT @ID:=@ID+1 as ID,t.*
FROM
(
SELECT pp.capitalid, pp.totallimit
FROM table1 pp
INNER JOIN table2 ppc ON pp.capitalid = ppc.capitalid
WHERE
pp.isduifu = 0
and pp.inserttimestamp <='2019-05-31 23:59:59'
and ppc.faxingjigou like '%R360%'
ORDER BY pp.totallimit
) t
) a where IF( ROUND(@ID/2,0)%2=0,ID in (ROUND(@ID/2,0),ROUND(@ID/2,0) + 1), ID=ROUND(@ID/2,0) );


本文参考链接:https://www.cnblogs.com/maohuidong/p/11008644.html