Question

I am working on a database for a student organization. One feature of the database is to record student attendance for events. If a student does not attend an event, their lack of attendance is not entered. In order for a student to attend an event they must be enrolled. The following tables are utilized for this process:

STUDENT
Student_ID int (pk),
Student_M,
First_Name,
Last_Name,
Gender,
Email,
Phone,
Degree,
Grad_Term,
Grad_Year,

STUDENT_ENROLLMENT
Enrollment_ID (pk),
Student_ID (fk),
Term,
Year,
Status,

STUDENT_ATTEND
Att_ID (pk),
Enrollment_ID (fk),
Event_ID (fk),

EVENT
Event_ID (pk),
Event_name,
Location,
Term,
Year,
Date,
Time,
Description,
Cost,
Dress_code,
Require,

My goal is to write a query that will display the lack of attendance. In this query I am trying to pull the student's name and email and the list of all events they did NOT attend which were required (WHERE event.require = 'Y'). I have tried multiple ways of writing this, many of which include numerous views, but with no luck. If anyone has any creative thoughts here it could really help!

Thanks

Was it helpful?

Solution

You need to cross join between students and events to get all possible combinations of those two. Then filter required events where there is no link between student and event.

select * from student s cross join event e
where e.Require = 1
  and not exists 
  (
    select * from STUDENT_ATTEND sa
      inner join STUDENT_ENROLLMENT se
      on se.Enrollment_ID = sa.Enrollment_ID
    where sa.Event_ID = e.Event_ID
      and se.Student_ID = s.Student_ID
  )
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top