我有以下两个表:
(我为在这里无法正确粘贴它们而道歉,因此创建了快照并给出了链接):
表格考试
+---------+------------+-------------+
| exam_id | exam_date | description |
+---------+------------+-------------+
| 1 | 2016-06-01 | Exam 1 |
| 2 | 2016-06-06 | Exam 2 |
| 3 | 2016-06-07 | Exam 3 |
| 4 | 2016-06-10 | Exam 4 |
+---------+------------+-------------+
表科目
+------------+---------+-------------+
| subject_id | exam_id | result |
+------------+---------+-------------+
| 1 | 1 | Attended |
| 2 | 1 | Fail |
| 3 | 2 | Distinction |
| 4 | 2 | Distinction |
| 5 | 3 | Pass |
| 6 | 3 | Distinction |
| 7 | 4 | Attended |
| 8 | 4 | Pass |
+------------+---------+-------------+
"结果"中的可能值领域是:
失败,出席,通过,区分
目的是根据该考试的下属科目取得的成绩来获得考试的总体结果 - "失败"在该考试中的一个科目中,意味着总体结果是“失败”#34; "出席"意味着该主题的结果尚不确定,除非任何其他主题已经导致了#34;失败"等级,结果只能被称为"参加"。同样,"区别"只有在该考试的所有科目中取得成绩时才能达到。
查询:
SELECT exams.exam_id, exam_date, description,
@count_fail := SUM(result='Fail') AS count_fail,
@count_attended := SUM(result='Attended') AS count_attended,
@count_pass := SUM(result='Pass') AS count_pass,
@count_distinction := SUM(result='Distinction') AS count_distinction,
CASE
WHEN (@count_fail >= @count_attended AND @count_fail >= 1)
THEN 'Fail'
WHEN (@count_attended >= @count_pass AND @count_attended >= 1)
THEN 'Attended'
WHEN (@count_pass >= @count_distinction AND @count_pass>= 1)
THEN 'Pass'
ELSE
'Distinction'
END AS final_result FROM exams
INNER JOIN subjects ON exams.exam_id = subjects.exam_id GROUP BY exam_id
结果令我难过......
+---------+------------+-------------+------------+----------------+------------+-------------------+--------------+
| exam_id | exam_date | description | count_fail | count_attended | count_pass | count_distinction | final_result |
+---------+------------+-------------+------------+----------------+------------+-------------------+--------------+
| 1 | 2016-06-01 | Exam 1 | 1 | 1 | 0 | 0 | Distinction |
| 2 | 2016-06-06 | Exam 2 | 0 | 0 | 0 | 2 | Fail |
| 3 | 2016-06-07 | Exam 3 | 0 | 0 | 1 | 1 | Distinction |
| 4 | 2016-06-10 | Exam 4 | 0 | 1 | 1 | 0 | Pass |
+---------+------------+-------------+------------+----------------+------------+-------------------+--------------+
我可以问一下这里发生了什么? WHEN条件是如何得不到的(很明显在@变量中进行了某种计算,因为输出不是所有的乱码)?这只是对更大的事物计划的一个小规模说明,遗憾的是我不能进入更精细的细节。但是,我真的想避免使用嵌套查询并尽可能多地使用JOINS ......
我非常感谢收到您的意见和建议。
非常感谢!
修改
数据库和查询的SQL小提琴是here
预期的输出是:
+---------+------------+-------------+------------+----------------+------------+-------------------+--------------+
| exam_id | exam_date | description | count_fail | count_attended | count_pass | count_distinction | final_result |
+---------+------------+-------------+------------+----------------+------------+-------------------+--------------+
| 1 | 2016-06-01 | Exam 1 | 1 | 1 | 0 | 0 | Fail |
| 2 | 2016-06-06 | Exam 2 | 0 | 0 | 0 | 2 | Distinction |
| 3 | 2016-06-07 | Exam 3 | 0 | 0 | 1 | 1 | Pass |
| 4 | 2016-06-10 | Exam 4 | 0 | 1 | 1 | 0 | Attended |
+---------+------------+-------------+------------+----------------+------------+-------------------+--------------+
理由是:
第1行:至少有一个科目达到了可能的最低等级(失败),因此总体结果必须是"失败"
第2行:该考试中的两个科目都已通过区分等级被清除,因此整体结果应该是"区别"
第3行: 1次传球,1次优异。这个结果不是一个区别,但是,它并不是一个失败的"无论是。由于此考试中的两个科目都已被清除(虽然其中一个科目没有区分等级),结果应为" Pass"。
第4行: 1参加,1次通过。计数不足以得出结论总体结果将是" Pass" (因为"参加"是不确定的)。因此,总体结果仅符合“参加者”的标准。 (直到考试中的所有科目都被#34; Pass"或者至少有一个"失败"清除,从而导致整个结果被调用&# 34;失败"。目前,"区别"现在无法实现,因为此考试中至少有一个科目已经注册了低级别成绩)
答案 0 :(得分:1)
这里是solution as shown in SQLFiddle没有用户定义的变量
SELECT exams.exam_id, exam_date, description,
SUM(result='Fail') AS count_fail,
SUM(result='Attended') AS count_attended,
SUM(result='Pass') AS count_pass,
SUM(result='Distinction') AS count_distinction,
CASE
WHEN (SUM(result='Fail') >= SUM(result='Attended') AND SUM(result='Fail') >= 1)
THEN 'Fail'
WHEN (SUM(result='Attended') >= SUM(result='Pass') AND SUM(result='Attended') >= 1)
THEN 'Attended'
WHEN (SUM(result='Pass') >= SUM(result='Distinction') AND SUM(result='Pass') >= 1)
THEN 'Pass'
ELSE
'Distinction'
END AS final_result FROM exams
INNER JOIN subjects ON exams.exam_id = subjects.exam_id
GROUP BY exam_id