0

I want to calculate how many (percent) of people did worse then I did on my tests.

Here is the result i want to have:

 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

I simply dont have a clue how to approach this, googling percentile results always ends up in something like this:

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

any ideas?

1
  • Could you edit the question to include the SQL you ran to get the second example?
    – IMSoP
    Jan 8, 2015 at 12:05

2 Answers 2

2

You can just use rolling count. So if you want to calculate how many (percent) of people did worse then I did on my tests, use this query:

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

But results are not exactly the same as your results, because from my point of view, your results are not showing how many (percent) of people did worse then I did on my tests, check, for example, ILNUX1 test - there're 4 people, 3 of these have 7.4 and one has 3.4, how percentilscore could be 33?

1
  • However your query couldn't have worked (i thought my question was clear at the time but it seems kinda not) thank you for the answer, it made me thinking and got me a lot further!
    – Randy
    Jan 16, 2015 at 10:18
1

Build up the query piece by piece i.e. you need to know the number of people with a worse score, then the number of others taking the test, then combine those two with a join...

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
1
  • However your query couldn't have worked (i thought my question was clear at the time but it seems kinda not) thank you for the answer, it made me thinking and got me a lot further!
    – Randy
    Jan 16, 2015 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.