2

美好时光。

比如说,有一个表包含有关考试结果的数据:

                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人最终通过)

4

3 回答 3

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
于 2013-04-30T07:57:44.537 回答
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')
于 2013-04-30T08:02:38.100 回答
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

SQL 小提琴演示

于 2013-04-30T08:10:02.813 回答