0

tbl 分数:(csv;我不知道如何在此处格式化表模式)

test_id, student_id, score   
1, 1, 75  
1, 2, 84  
1, 3, 60  
2, 1, 82  
1, 4, 90  
1, 5, 75  
2, 2, 70  
2, 3, 90  
2, 5, 76   

我需要做的是,对于每个分数的每个测试,计算该分数的学生人数,然后找出每个测试的分数是多少百分比。

我使用查询成功计算了每个分数的学生人数:

SELECT test_id, score, COUNT(student_id) as num_students FROM Scores GROUP BY test_id, score

在这一点上,我试图为每个测试的每个分数获取#at、#below 和#above,以便我可以尝试计算每个分数的百分位数。

这会是某种数据透视表吗?

4

2 回答 2

0

在 MySQL 中,您需要使用相关子查询来执行此操作。以下是学生人数的示例:

 SELECT test_id, score, COUNT(student_id) as num_students,
        (select count(*) from scores s2 where s2.test_id = s.test_id and s2.score < s.score
        ) as below
 FROM Scores s
 GROUP BY test_id, score
于 2013-03-05T02:36:27.877 回答
0

它不一定是相关的,但由于交叉连接和不同,它可能会表现更差我不确定它会表现得更好或更差

SELECT s.test_id, 
       s.score, 
       Count(DISTINCT s.student_id) AS num_students, 
       Count(DISTINCT z.student_id) AS below 
FROM   scores s 
       LEFT JOIN scores z 
              ON s.test_id = z.test_id 
                 AND s.score > z.score 
GROUP  BY s.test_id, 
          s.score 
ORDER  BY s.test_id, 
          s.score; 

http://sqlfiddle.com/#!2/fd824/13

于 2013-03-05T02:58:34.833 回答