我需要计算这样的值列表的第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
答案 0 :(得分:4)
百分位数(或百分位数)是统计中使用的度量,指示一组观察值中观察到的给定百分比下降的值。例如,第20百分位数是低于其中20%的观察结果的值(或分数)。
百分位数没有标准定义
最近等级方法:
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;
答案 1 :(得分:1)
0.0713不是正确答案,procentile是列表中的值
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 max(case when rownum*1.0/numrows <= 0.9 then DATA end) as percentile_90th
from (select DATA,
row_number() over (order by DATA) as rownum,
count(*) over (partition by NULL) as numrows
from @Temp
where DATA is not null
) t
FROM T-SQL: Calculating the Nth Percentile Value from column
答案 2 :(得分:0)
PERCENTILE_CONT功能:
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
PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY e.DATA) OVER () AS PERCENTILE
FROM @Temp AS e