简体   繁体   中英

How to find all students who have more than one exams in same date for whole period of exams

I have table consists of student number, subject, exam_date, and I can find the students who have more than one exam by using this query

SELECT 
     student_number, subject, exam_date
FROM 
     tables
WHERE 
    dates ='01-01-2019' And 
    student_number IN (
         SELECT student_number
         FROM tables WHERE dates ="01-01-2019"
         GROUP BY student_number
         HAVING COUNT(student_number) > 1
         )  

However, I want to find all students who have more than one exams on the same date for the whole period of the exams (date list) instead of typing each date individually.

You are pretty close with your group by.

SELECT student_number, exam_date
FROM tables
GROUP BY student_number, exam_date
HAVING count(*)>1

This should get you started, you had the right idea with your GROUP BY...

I guess if a student has more than one exams on the same date, these exams would be on different subjects.
So you can use get what you need with EXISTS:

SELECT student_number, subject, exam_date
FROM tables t
WHERE EXISTS (
  SELECT 1 FROM tables
  WHERE 
    student_number = t.student_number
    AND
    exam_date = t.exam_date
    AND
    subject <> t.subject
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM