mySQL求中位数、众数

mysql 取中位数、众数

分数表

-- 求众数
SELECT score, count(score) from scores 
 GROUP BY score
HAVING count(score) >=
(SELECT max(a.b) from 
 (SELECT count(score) as b from scores
GROUP BY score) a )

思路:
对数据进行排序,利用id确定中位数的位置,最大id跟最小id取平均值即中位数。
– 数据量为基数,平均值刚好为中位数
– 数据量为偶数,平局值通过FLOOR() ceil () 函数可定位到中位数,取均值即可。

1.如自带ID

SELECT AVG(score) from 
(SELECT score , id FROM scores ) a 
  WHERE id in (SELECT FLOOR(((MAX(id)+MIN(id))/2)) as m from scores b UNION
SELECT CEIL(((MAX(id)+MIN(id))/2)) as m from scores c );

2.如果没有ID

-- 求中位数
-- 申明变量
-- @rowindex := @rowindex + 1 自增长
-- FLOOR()向下取整 ceil 向上取整

set @rowindex := -1;
SELECT AVG(a.score) from (
 SELECT (@rowindex := @rowindex + 1) as rowindex,score from scores
ORDER BY score )a 
 WHERE rowindex in (FLOOR(@rowindex/2+1),CEIL(@rowindex/2+1))
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MySQL中,计算中位数的方法有多种。以下是几种常用的方法: 方法一:使用窗口函数 可以使用窗口函数来计算中位数。具体的查询语句如下: ``` select sum(score) / count(*) as median from ( select a score, row_number() over (order by a desc,id desc) as desc_math, row_number() over (order by a asc, id asc) as asc_math from t1 ) as order_table where asc_math in (desc_math, desc_math + 1, desc_math - 1); ``` 这种方法的优点是只需要扫描一次表,性能较好。但需要注意的是,使用窗口函数需要MySQL 8以上版本的支持,并且row_number()中的order by值必须唯一,否则在遇到重复值的情况下结果可能不正确。\[1\] 方法二:使用GROUP_CONCAT和AVG函数 如果需要计算偶数个数的中位数,可以使用以下查询语句: ``` SELECT GROUP_CONCAT(id), AVG(VALUE) FROM ( SELECT id, VALUE, @index := @index + 1 myIndex FROM student, (SELECT @index := 0) a ORDER BY VALUE ) b WHERE FLOOR(@index / 2+1) = myIndex OR CEIL(@index / 2) = myIndex; ``` 这种方法会将所有的值按照升序排序,并使用GROUP_CONCAT函数将id连接成一个字符串,然后使用AVG函数计算平均值。\[2\] 方法三:使用子查询和变量 另一种方法是使用子查询和变量来计算中位数。具体的查询语句如下: ``` select avg(a) from ( select a,@a:=@a+1 b from t1,(select @a:=0) t2 order by a ) t where b between @a/2 and @a/2+1; ``` 这种方法会将所有的值按照升序排序,并使用变量@a来记录行号,然后使用子查询计算平均值。\[3\] 以上是在MySQL计算中位数的几种常用方法。根据具体的需和数据结构,可以选择适合的方法来计算中位数。 #### 引用[.reference_title] - *1* *3* [MySQL查询中位数最简单的写法](https://blog.csdn.net/wzy0623/article/details/127284099)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [msyql 计算中位数](https://blog.csdn.net/weixin_42056745/article/details/107086171)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天山下小花家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值