SQL 计算第 95 个百分位值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6784299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculating the 95th Percentile value?
提问by Legend
I am trying to calculate the 95th percentile point in my table column and am using the TOP 95 PERCENT
construct. I was wondering if the following approach is correct or if there is a better way of doing the same.
我正在尝试计算表列中的第 95 个百分位点,并且正在使用该TOP 95 PERCENT
构造。我想知道以下方法是否正确,或者是否有更好的方法来做同样的事情。
SELECT AVG(Value)
FROM
(
SELECT TOP 95 PERCENT WITH TIES Value
FROM [Sample].[dbo].[numbers]
ORDER BY Value DESC
) AS NineP
采纳答案by Legend
That would get you the (roughly) 97.5 percentile point, because you're taking the average of the top 5%. You want to SELECT MIN(Value)
这将使您获得(大约)97.5 个百分点,因为您取的是前 5% 的平均值。你想要SELECT MIN(Value)
回答by jons911
When you get to SQL Server 2012, PERCENTILE_DISCand PERCENTILE_CONTfunctions solve this in a single query.
当您使用 SQL Server 2012 时,PERCENTILE_DISC和PERCENTILE_CONT函数可在单个查询中解决此问题。
回答by VISQL
Ntile is where the data is divided into that "tile" where we can think of the tile having a size, and all those sizes being the same for each tile. For your 95th percentile, you want the place where the data is divided for the 95th time. That would be the START of the 95th percentile or the MIN, not the MAX.
Ntile 是将数据划分为“tile”的地方,我们可以在其中认为 tile 具有大小,并且每个 tile 的所有这些大小都相同。对于第 95 个百分位数,您需要第 95 次划分数据的位置。那将是第 95 个百分位数或 MIN 的 START,而不是 MAX。