美好时光。
说,有一个表格包含有关考试结果的数据:
EXAM_RESULTS
| ID | EXAM_TYPE | PERSON_ID | EXAM_RESULT |
学生可以尝试多次通过具体考试,因此一个学生可以在上表中有四行:三个用于失败结果,一个用于成功。我需要选择尚未通过具体考试类型的所有学生。
请建议,如何操作或在何处阅读有关此类技巧的内容。
修改: 样本记录:
| 1 | SDA | 111 | FAIL |
| 2 | SDA | 111 | FAIL |
| 3 | SDA | 111 | PASSED |
| 4 | SDA | 222 | FAIL |
| 4 | SDA | 222 | FAIL |
根据任务,查询必须只选择222人因为他没有通过SDA考试(111人最终通过)
答案 0 :(得分:2)
SELECT Person_ID
FROM Exam_Result
WHERE Exam_type = 'type_here'
GROUP BY Person_ID
HAVING COUNT(CASE WHEN EXAM_RESULT = 'PASSED' THEN 1 END) = 0
答案 1 :(得分:1)
/* Students that not have passed the exam: either haven't taken it or failed all their's attempts */
/* All the students */
select distinct Person_id
from Exam_Results
except /* this keyword is server dependent! MINUS for Oracle*/
/* Students that have passed */
select Person_Id
from Exam_Results
where (Exam_Result = 'passed') and
(Exam_Type = 'my exam type')
答案 2 :(得分:1)
只是为了让您先行一步,这就是您可以做的事情:
SELECT PersonId,
COUNT(CASE WHEN ExamResult = 'PASS' THEN 1 END) CNT
FROM ExamResults
WHERE ExamType = 'CONCRETE'
GROUP BY PersonId
HAVING COUNT(CASE WHEN ExamResult = 'PASS' THEN 1 END) > 0