按分区计算百分等级

时间:2019-03-06 17:37:34

标签: mysql

我正在使用mysql5.6,并且试图按分区计算百分等级。我找到了这个问题Calculating percentile rank in MySQL,并使用了Conor解决方案和这个问题Rank function in MySQL。我能够计算分区等级,但请注意百分位等级。我的百分位排名不正确,我想知道如何解决这个问题。

示例

CREATE TABLE sandbox.test_person (
person_id INT NOT Null PRIMARY KEY,
person_condition VARCHAR(25),
score FLOAT8
);

虚拟数据

INSERT INTO sandbox.test_person (person_id, person_condition, score) VALUES
(1,  'A',   3.3),
(2,  'A',   3.1),
(3,  'B',   2.5),
(4,  'A.A', 3.5),
(5,  'B',   3.0),
(6,  'B.A', 3.0),
(7,  'B.A', 3.0),
(8,  'B',   2.7),
(9,  'A',   3.0),
(10, 'A.A', 2.6),
(11, 'B.B', 2.0),
(12, 'A.A', 2.0),
(13, 'A',   2.1),
(14, 'B.A', 3.5),
(15, 'A',   3.2),
(16, 'B',   2.5),
(17, 'B.B', 2.7),
(18, 'B.A', 3.5),
(19, 'A.A', 3.5),
(20, 'B.A', 2.5);

查询以获取排名和百分位数排名:

SELECT 
    c.person_id,
    c.person_condition,
    c.score,
    rnk,
    ROUND(((@rank - rnk) / (@rank +1 )) * 100, 4) AS percentile_rank
FROM
    (SELECT
        *,
        @rank := CASE
            WHEN @partval = sandbox.test_person.person_condition AND @rankval = sandbox.test_person.score THEN @rank
            WHEN @partval = sandbox.test_person.person_condition AND (@rankval:= sandbox.test_person.score) IS NOT NULL THEN @rank + 1
            WHEN (@partval:= sandbox.test_person.person_condition) IS NOT NULL AND (@rankval:= sandbox.test_person.score) IS NOT NULL THEN 1
        END AS rnk
    FROM 
        sandbox.test_person, 
        (SELECT @rank := NULL, @partval := NULL, @rankval := NULL) AS x
ORDER BY test_person.person_condition, test_person.score DESC) AS c;

输出

Peron_id | person_condition | score | rank | percentile_rank
  13     |        A         |  2.1  |   1  |      33.3333   
  9      |        A         |  3    |   2  |       0 
  2      |        A         |  3.1  |   3  |     -33.3333 
  15     |        A         |  3.2  |   4  |     -66.6667 
  1      |        A         |  3.3  |   5  |    -100 
  4      |        A.A       |  2    |   1  |      33.3333 
  12     |        A.A       |  2    |   1  |      33.3333 
  10     |        A.A       |  2.6  |   2  |       0 
  19     |        A.A       |  3.5  |   3  |     -33.3333 
  3      |        B         |  2.5  |   1  |      33.3333 
  16     |        B         |  2.5  |   1  |      33.3333 
  8      |        B         |  2.7  |   2  |       0 
  5      |        B         |  3    |   3  |     -33.3333 
  20     |        B.A       |  2.5  |   1  |      33.3333 
  7      |        B.A       |  3    |   2  |       0 
  6      |        B.A       |  3    |   2  |       0 
  14     |        B.A       |  3.5  |   3  |     -33.3333 
  18     |        B.A       |  3.5  |   3  |     -33.3333 
  11     |        B.B       |  2    |   1  |      33.3333 
  17     |        B.B       |  2.7  |   2  |       0  

所需结果

我正在寻找一种准确计算百分位数排名的方法,以使上表类似于:

Peron_id | person_condition | score | rank | percentile_rank
  13     |        A         |  2.1  |   5  |       0   
  9      |        A         |  3    |   4  |      25 
  2      |        A         |  3.1  |   3  |      50
  15     |        A         |  3.2  |   2  |      75 
  1      |        A         |  3.3  |   1  |     100 
  4      |        A.A       |  2    |   3  |       0 
  12     |        A.A       |  2    |   3  |       0 
  10     |        A.A       |  2.6  |   2  |      50 
  19     |        A.A       |  3.5  |   1  |     100 
  3      |        B         |  2.5  |   3  |       0 
  16     |        B         |  2.5  |   3  |       0 
  8      |        B         |  2.7  |   2  |      50 
  5      |        B         |  3    |   1  |     100 
  20     |        B.A       |  2.5  |   3  |       0 
  7      |        B.A       |  3    |   2  |      50 
  6      |        B.A       |  3    |   2  |      50 
  14     |        B.A       |  3.5  |   1  |     100 
  18     |        B.A       |  3.5  |   1  |     100
  11     |        B.B       |  2    |   2  |       0 
  17     |        B.B       |  2.7  |   1  |     100 

0 个答案:

没有答案