mysql数据库众数查询_mysql中位数、众数计算

本文介绍了如何在MySQL中计算众数和中位数,以数据表score为例,展示了利用子查询和聚合函数实现这两个统计量的具体SQL语句。对于中位数,通过比较每个分数出现的频率找到中间值;对于众数,通过count(*)>=all子句找出出现次数最多的分数。
摘要由CSDN通过智能技术生成

平均数、中位数、众数常用来描述数据的集中程度,在mysql中,均值的计算较为简单,但中位数和众数尚不支持可以直接计算的函数,在这里向大家介绍一下中位数和众数的计算方法。

本文所使用的数据表为score,包含三个字段:

s_id 为学生id

c_id 为课程id

s_score 为课程成绩

1、中位数

查找课程编号为‘02’号的学生成绩中位数

课程编号为‘02’的课程成绩为

mysql> select s_score from score where c_id='02';

+---------+

| s_score |

+---------+

| 90 |

| 60 |

| 80 |

| 30 |

| 87 |

| 89 |

+---------+

6 rows in set (0.00 sec)

查找中位数

mysql> select avg(c.s_score) from(

-> select a.s_score from score a,score b

-> where a.c_id=b.c_id and a.c_id='02'

-> group by a.s_score

-> having sum(case when a.s_score=b.s_score then 1 else 0 end)

-> >= abs(sum(sign(a.s_score-b.s_score)))

-> )c;

+----------------+

| avg(c.s_score) |

+----------------+

| 83.5000 |

+----------------+

1 row in set (0.00 sec)

解析:

当把having条件作为查询内容时,便比较清晰了,结果如下

mysql> select a.s_score,

-> sum(case when a.s_score=b.s_score then 1 else 0 end )as if_equal,

-> abs(sum(sign(a.s_score-b.s_score)))

-> from score a,score b where a.c_id=b.c_id and a.c_id='02'

-> group by a.s_score;

+---------+----------+-------------------------------------+

| s_score | if_equal | abs(sum(sign(a.s_score-b.s_score))) |

+---------+----------+-------------------------------------+

| 30 | 1 | 5 |

| 60 | 1 | 3 |

| 80 | 1 | 1 |

| 87 | 1 | 1 |

| 89 | 1 | 3 |

| 90 | 1 | 5 |

+---------+----------+-------------------------------------+

6 rows in set (0.00 sec)

2、众数

一组数据可以存在多个众数

mysql> select s_score from score where c_id='02';

+---------+

| s_score |

+---------+

| 90 |

| 60 |

| 80 |

| 30 |

| 80 |

| 89 |

+---------+

6 rows in set (0.00 sec)

mysql> select s_score from score

-> where c_id='02'

-> group by s_score

-> having count(*)

-> >= all(select count(*) from score where c_id='02' group by s_score);

+---------+

| s_score |

+---------+

| 80 |

+---------+

1 row in set (0.00 sec)

解析:

mysql> select s_score,count(*) from score where c_id='02' group by s_score;

+---------+----------+

| s_score | count(*) |

+---------+----------+

| 30 | 1 |

| 60 | 1 |

| 80 | 2 |

| 89 | 1 |

| 90 | 1 |

+---------+----------+

5 rows in set (0.00 sec)

count() >= all(select count() from score where c_id='02' group by s_score)

表示score中分组后某个s_score出现的次数 >= 所有不同s_score出现的次数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值