[英]Finding the largest score differential in SQL
I am trying to write a query to find the largest point differential for students before and after taking a course.我正在尝试编写一个查询来查找学生在上课前后的最大点差。 I need the user_id of the person with the largest score differential for every course (there are around 500).我需要每门课程得分差异最大的人的 user_id(大约有 500 个)。
The columns that are provided in the table include: id, user_id, course_id, #_questions_correct, #_questions, and quiz_type.表中提供的列包括:id、user_id、course_id、#_questions_correct、#_questions 和 quiz_type。 The options pre, and post exist in the quiz_type column. pre 和 post 选项存在于 quiz_type 列中。
I cannot seem to find a way to do this efficiently, so I would love to hear suggestions on making this easier!我似乎找不到有效执行此操作的方法,所以我很想听听有关简化此操作的建议!
So far all I have come up with is:到目前为止,我想出的是:
SELECT user_id, course_id (number_correct / number_of_questions)*100 AS test_score, quiz_type
FROM results
WHERE quiz_type = 'pre'
ORDER BY test_score;
I don't know if this is a good start or what, let me know!我不知道这是一个好的开始还是什么,让我知道!
This should work.这应该工作。 Note you might need to make syntax changes - I don't know postgresql so this is formatted for SQL Server.请注意,您可能需要进行语法更改 - 我不知道 postgresql,所以这是为 SQL 服务器格式化的。
SELECT user_id, course_id, postscore-prescore AS differential
FROM
--Get all the prescores
(
SELECT user_id, course_id, (1.0)*num_questions_correct/num_questions AS prescore
FROM @input
WHERE quiz_type = 1 --quiz_type = pre
) AS a
INNER JOIN
--Get all the postscores
(
SELECT user_id AS tmp, course_id AS tmp2, (1.0)*num_questions_correct/num_questions AS postscore
FROM @input
WHERE quiz_type = 2 -- quiz_type = post
) AS b
--Put the prescores and postscores in the same table as separate columns
ON a.user_id=b.tmp AND a.course_id=b.tmp2
ORDER BY differential DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.