作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要计算这样的值列表的第 90 个百分位数:
0.0099
0.0129
0.0031
0.0219
0.2632
0.0124
0.0493
0.05
0.0433
0.0713,9
.有什么建议?
DECLARE @Temp TABLE(DATA float)
INSERT INTO @Temp VALUES(0.0099)
INSERT INTO @Temp VALUES(0.0129)
INSERT INTO @Temp VALUES(0.0031)
INSERT INTO @Temp VALUES(0.0219)
INSERT INTO @Temp VALUES(0.2632)
INSERT INTO @Temp VALUES(0.0124)
INSERT INTO @Temp VALUES(0.0493)
INSERT INTO @Temp VALUES(0.05)
INSERT INTO @Temp VALUES(0.0433)
SELECT DATA
FROM @Temp
ORDER BY DATA ASC
--90th percentile
SELECT ((
SELECT TOP 1 DATA
FROM (
SELECT TOP 90 PERCENT DATA
FROM @Temp
WHERE DATA IS NOT NULL
ORDER BY DATA
) AS A
ORDER BY DATA DESC) +
(
SELECT TOP 1 DATA
FROM (
SELECT TOP 10 PERCENT DATA
FROM @Temp
WHERE DATA IS NOT NULL
ORDER BY DATA DESC
) AS A
ORDER BY DATA ASC)) / 2.0
最佳答案
A percentile (or a centile) is a measure used in statistics indicating the value below which a given percentage of observations in a group of observations fall. For example, the 20th percentile is the value (or score) below which 20 percent of the observations may be found.
There is no standard definition of percentile
SELECT DATA
FROM (
SELECT
DATA,
COUNT(1) OVER (PARTITION BY NULL) As N,
ROW_NUMBER() OVER (ORDER BY DATA) AS i
FROM @Temp) t
WHERE
i = ROUND(N * 90.00 / 100.00, 0, 0)
DECLARE @P real = 90.00
SELECT MAX(tt.Pv)
FROM (
SELECT
(CASE
WHEN i = k THEN DATA
WHEN k = 0 AND P = MIN(CASE WHEN P > @P THEN P END) OVER (PARTITION BY NULL) THEN
DATA + N * (@P - P) / 100 * (MIN(CASE WHEN P > @P THEN DATA END) OVER (PARTITION BY NULL) - DATA)
ELSE 0
END) AS Pv
FROM (
SELECT
*,
100.00 / N * (i - 1.00 / 2.00) AS P,
CASE
WHEN @P < 100.00 / N * (1 - 1.00 / 2.00) THEN 1
WHEN @P > 100.00 / N * (N - 1.00 / 2.00) THEN N
WHEN @P = 100.00 / N * (i - 1.00 / 2.00) THEN i
WHEN @P > 100.00 / N * (i - 1.00 / 2.00) THEN i
ELSE 0
END AS k
FROM (
SELECT
DATA,
COUNT(*) OVER (PARTITION BY NULL) As N,
ROW_NUMBER() OVER (ORDER BY DATA) AS i
FROM @Temp) ti) t
) tt;
关于sql - 如何计算 SQL Server 中的第 90 个百分位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341793/
我是一名优秀的程序员,十分优秀!