用MySQL计算中位数问题,怎么解决

用MySQL计算中位数问题,怎么解决,第1张

中位数:也就是选取中间的数。一种衡量集中趋势的方法。

要找中位数,首先需要从小到大排序,例如这组数据:23、29、20、32、23、21、33、25;

我们将数据排序20、21、23、23、25、29、32、33;排序后发现有8个数怎么办?

若有n个数,n为奇数,则选择第(n+1)/2个为中位数,若n为偶数,则中位数是(n/2以及n+1/2)的平均数

此例中选择24为中位数

有点复杂,在你基础上加了条有奇数的数据

创建表,插入数据:

create table test

(cat_id int,

price int)

insert into test values (101,90)

insert into test values (101,99)

insert into test values (102,98)

insert into test values (103,96)

insert into test values (102,95)

insert into test values (102,94)

insert into test values (102,93)

insert into test values (103,99)

insert into test values (103,98)

insert into test values (103,97)

insert into test values (104,96)

insert into test values (104,95)

insert into test values (105,97)

insert into test values (105,96)

insert into test values (105,95)

执行:

SELECT

t1.cat_id,

round(avg(t1.price), 1) price

FROM

(

SELECT

*

FROM

(

SELECT

t.cat_id,

t.price,

count(*) AS rank

FROM

test t

LEFT OUTER JOIN test r ON t.cat_id = r.cat_id

AND t.price <= r.price

GROUP BY

t.cat_id,

t.price

ORDER BY

t.cat_id,

t.price DESC

) s

) t1,

(

SELECT DISTINCT

a.cat_id,

round(a.maxrank / 2) rank

FROM

(

SELECT

cat_id,

max(rank) maxrank,

MOD (max(rank), 2) modrank

FROM

(

SELECT

*

FROM

(

SELECT

t.cat_id,

t.price,

count(*) AS rank

FROM

test t

LEFT OUTER JOIN test r ON t.cat_id = r.cat_id

AND t.price <= r.price

GROUP BY

t.cat_id,

t.price

ORDER BY

t.cat_id,

t.price DESC

) s

) t1

GROUP BY

cat_id

) a,

(

SELECT

*

FROM

(

SELECT

t.cat_id,

t.price,

count(*) AS rank

FROM

test t

LEFT OUTER JOIN test r ON t.cat_id = r.cat_id

AND t.price <= r.price

GROUP BY

t.cat_id,

t.price

ORDER BY

t.cat_id,

t.price DESC

) s

) b

WHERE

a.cat_id = b.cat_id

AND a.modrank = 0

UNION ALL

SELECT DISTINCT

a.cat_id,

round(a.maxrank / 2) + 1 rank

FROM

(

SELECT

cat_id,

max(rank) maxrank,

MOD (max(rank), 2) modrank

FROM

(

SELECT

*

FROM

(

SELECT

t.cat_id,

t.price,

count(*) AS rank

FROM

test t

LEFT OUTER JOIN test r ON t.cat_id = r.cat_id

AND t.price <= r.price

GROUP BY

t.cat_id,

t.price

ORDER BY

t.cat_id,

t.price DESC

) s

) t1

GROUP BY

cat_id

) a,

(

SELECT

*

FROM

(

SELECT

t.cat_id,

t.price,

count(*) AS rank

FROM

test t

LEFT OUTER JOIN test r ON t.cat_id = r.cat_id

AND t.price <= r.price

GROUP BY

t.cat_id,

t.price

ORDER BY

t.cat_id,

t.price DESC

) s

) b

WHERE

a.cat_id = b.cat_id

AND a.modrank = 0

UNION ALL

SELECT DISTINCT

a.cat_id,

round(a.maxrank / 2) rank

FROM

(

SELECT

cat_id,

max(rank) maxrank,

MOD (max(rank), 2) modrank

FROM

(

SELECT

*

FROM

(

SELECT

t.cat_id,

t.price,

count(*) AS rank

FROM

test t

LEFT OUTER JOIN test r ON t.cat_id = r.cat_id

AND t.price <= r.price

GROUP BY

t.cat_id,

t.price

ORDER BY

t.cat_id,

t.price DESC

) s

) t1

GROUP BY

cat_id

) a,

(

SELECT

*

FROM

(

SELECT

t.cat_id,

t.price,

count(*) AS rank

FROM

test t

LEFT OUTER JOIN test r ON t.cat_id = r.cat_id

AND t.price <= r.price

GROUP BY

t.cat_id,

t.price

ORDER BY

t.cat_id,

t.price DESC

) s

) b

WHERE

a.cat_id = b.cat_id

AND a.modrank = 1

) t2

WHERE

t1.cat_id = t2.cat_id

AND t1.rank = t2.rank

GROUP BY

t1.cat_id

结果:

其中:

select * from (  

select t.cat_id,t.price,count(*) as rank from test t  

LEFT OUTER JOIN test r  

on t.cat_id = r.cat_id  

and t.price<=r.price  

group by t.cat_id,t.price  

order by t.cat_id, t.price desc  

) s

这条是主语句,主要是按照大小给出一个排名,然后根据中位数的公式,偶数的话,取最中间两个的平均数,奇数取最中间的数。自己研究一下吧。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7193322.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-02
下一篇 2023-04-02

发表评论

登录后才能评论

评论列表(0条)

保存