Select nth percentile from MySQL(从 MySQL 中选择第 n 个百分位)
问题描述
我有一个简单的数据表,我想从查询中选择大约第 40 个百分位的行.
I have a simple table of data, and I'd like to select the row that's at about the 40th percentile from the query.
我现在可以通过首先查询找到行数然后运行另一个查询来排序和选择第 n 行:
I can do this right now by first querying to find the number of rows and then running another query that sorts and selects the nth row:
select count(*) as `total` from mydata;
可能返回类似 93, 93*0.4 = 37 的内容
which may return something like 93, 93*0.4 = 37
select * from mydata order by `field` asc limit 37,1;
我可以将这两个查询合并为一个查询吗?
Can I combine these two queries into a single query?
推荐答案
这将为您提供大约第 40 个百分位数,它返回 40% 的行小于它的行.它根据行距第 40 个百分位数的距离对行进行排序,因为没有一行可能恰好落在第 40 个百分位数上.
This will give you approximately the 40th percentile, it returns the row where 40% of rows are less than it. It sorts rows by how far they are from the 40th percentile, since no row may fall exactly on the 40th percentile.
SELECT m1.field, m1.otherfield, count(m2.field)
FROM mydata m1 INNER JOIN mydata m2 ON m2.field<m1.field
GROUP BY
m1.field,m1.otherfield
ORDER BY
ABS(0.4-(count(m2.field)/(select count(*) from mydata)))
LIMIT 1
这篇关于从 MySQL 中选择第 n 个百分位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 MySQL 中选择第 n 个百分位
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
