选择 MySQL 中数值的前 X(或后)百分比
我想知道 MySQL 中是否可以使用任何函数从包含数值的列中选择前 X(或后)百分比。
基本上,我有一个包含价格列表的列,我只想返回价格前十个百分位中的那些字段。有什么建议吗?
I was wondering if there are any functions that can be used in MySQL to select the TOP X(or bottom) percent from a column containing numeric values.
Basically, I have a column containing a list of prices and I only want to return those fields in the top ten percentile of prices. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑 - 新答案
在 将 SQL Server 查询转换为 MySQL 中回答
旧答案
对于MySQL,您可以计算所需的批量大小,然后限制为该记录数
对于底部百分比,只需反向排序
哎呀,也许DESC属于第一个查询,但您明白了含义。
注意
对于 SQL Server,TOP N PERCENT 子句肯定有帮助
EDIT - new answer
Answered in Convert SQL Server query to MySQL
OLD ANSWER
For MySQL, you could calculate the batch size required and then LIMIT to that number of records
For a bottom percent, just order in reverse
Oops, maybe the DESC belongs in the first query, but you get the meaning.
Note
For SQL Server, the TOP N PERCENT clause certainly helps
仅供参考(我知道这个问题已经有几年了),这也可以通过其他更干净的方式来完成。
我在一个非常大的数据库上运行了类似的查询,并且运行得非常快。
just as an FYI (i know this question is a few years old), this can be done other, cleaner ways as well.
i ran a similar query over a very large database, and it ran very quickly.
更新:来自更了解的人对这个主题的更深思熟虑的解释这里。尽管如此,MySQL 中似乎仍然没有嵌入式函数来计算百分位数。
尝试:
SELECT * FROMpricesWHEREprice>= (SELECT 0.9 * max(price)FROMprices)这将为中的记录数给出价格P1具有 price >= P1 的 Price 表将是 Price 表中记录总数的十分之一。
之后:
将返回所有所需的记录。
注意:我没有检查这个查询的性能,我认为使用临时表/变量的解决方案一定更有效。
UPDATE: Much more thought-out explanation of the subject from much more knowing person here. Nonetheless, it still seems there's no embedded function in MySQL to calculate percentiles.
Try:
SELECT * FROM prices WHERE price >= (SELECT 0.9 * max(price) FROM prices)This will give price P1 for which number of records in Price table having price >= P1 will be one tenth of total number of records in Price table.
After that:
will return all desired records.
Note: I didn't examine performance of this query, I think solution with temporary table/variable must be more effective.