百分位得分,有多少人做得比postgresql差

时间:2015-01-08 11:13:14

标签: sql postgresql percentile

我想计算在测试中我做了多少(百分比)的人。

以下是我想要的结果:

 student | vak   | resultaat | percentielscore 
---------+----------+-----------+-----------------
 1000001 | IBUI  | 5.1 | 0
 1000001 | ILNUX1| 3.4 | 0
 1000001 | IMUML | 6.9 | 0
 1000001 | IRDB  | 2.5 | 0
 1000002 | IARCH | 7.8 | 0
 1000002 | ICOM  | 5.6 | 0
 1000002 | INST  | 6.2 | 0
 1000002 | IRDB  | 7.2 | 100
 1000003 | IARCH | 7.8 | 0
 1000003 | ILNUX1| 7.4 | 33
 1000003 | IMUML | 6.9 | 0
 1000003 | INST  | 6.2 | 0
 1000003 | IRDB  | 3.5 | 25
 1000004 | IBUI  | 9.5 | 100
 1000004 | ICOM  | 5.6 | 0
 1000004 | ILNUX1| 7.4 | 33
 1000004 | IRDB  | 3.5 | 25
 1000005 | ILNUX1| 7.4 | 33
 1000005 | IMHTB | 4.2 | 
 1000005 | IMUML | 6.9 | 0
 1000005 | INST  | 6.2 | 0
 1000005 | IRDB  | 4.8 | 75

我根本不知道如何处理这个问题,谷歌搜索百分位结果总是以这样的结果:

student | vak   | resultaat | percentielscore 
---------+----------+-----------+-----------------
"1000001";"ILNUX1  ";2.9;  2.40
"1000001";"IMUML   ";6.4;  5.29
"1000001";"IBUI    ";4.6;  3.80
"1000001";"IRDB    ";2.0;  1.65
"1000002";"INST    ";5.7;  4.71
"1000002";"IARCH   ";7.3;  6.03
"1000002";"IRDB    ";6.7;  5.54
"1000002";"ICOM    ";5.1;  4.21
"1000003";"IMUML   ";6.4;  5.29
"1000003";"IRDB    ";3.0;  2.48
"1000003";"INST    ";5.7;  4.71
"1000003";"IARCH   ";7.3;  6.03
"1000003";"ILNUX1  ";6.9;  5.70
"1000004";"IRDB    ";3.0;  2.48
"1000004";"ILNUX1  ";6.9;  5.70
"1000004";"ICOM    ";5.1;  4.21
"1000004";"IBUI    ";9.0;  7.44
"1000005";"IRDB    ";4.3;  3.55
"1000005";"ILNUX1  ";6.9;  5.70
"1000005";"IMUML   ";6.4;  5.29
"1000005";"IMHTB   ";3.7;  3.06
"1000005";"INST    ";5.7;  4.71

任何想法?

2 个答案:

答案 0 :(得分:2)

您可以使用滚动计数。因此,如果你想计算有多少(百分比)的人做得比我的测试更糟糕,请使用此查询:

with cte as (
    select
        student, vak, resultaat, percentielscore,
        count(*) over(partition by vak, resultaat) as result_count,
        count(*) over(partition by vak order by resultaat) as rolling_count,
        count(*) over(partition by vak) as total_count
    from Table1
    order by vak
)
select
    student, vak, resultaat, percentielscore,
    (rolling_count - result_count) * 100.0 / total_count as percentielscore2
from cte
order by student, vak

<强> sql fiddle demo

但结果与您的结果并不完全相同,因为从我的角度来看,您的结果并没有显示有多少(百分比)的人比我在测试时做的更差,检查例如,ILNUX1测试 - 有4个人,其中3个有7.4个,一个有3.4个,如何使用核心可以33个?

答案 1 :(得分:1)

逐个建立查询,即您需要知道得分较差的人数,然后是其他参加测试的人数,然后将这两个人加入一个加入......

SELECT s.student,
s.vak,
s.resultaat,
worse_score_count,
number_of_tests,
COALESCE(worse_score_count,0) / number_of_tests::double precision as percentiel
FROM
percentages s

INNER JOIN LATERAL  -- use a lateral query to only count the other students

-- get the total number of tests by other students
    (SELECT vak,count(*) AS number_of_tests
    FROM percentages
    where student <> s.student
    GROUP BY vak) t
ON t.vak = s.vak

LEFT JOIN

-- get the number of students with worse test scores
    (SELECT p1.student,p1.vak,count(*)  AS worse_score_count 
    FROM percentages p1
    INNER JOIN percentages p2 ON p1.vak = p2.vak AND p2.percentielscore <p1.percentielscore
    GROUP BY p1.student,p1.vak) w
ON w.student = s.student and w.vak = s.vak
ORDER by s.student,s.vak