Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Count Records in Table Except If Related Record Exists in Child Table

$
0
0

Dear All

I have the following query that counts all of the records in the table SENAlert based upon the teacher's username in a related table.

SELECT COUNT(SENAlert.SENAlertID) AS Expr1 FROM Class INNER JOIN ClassMember ON Class.ClassClassCode = ClassMember.ClassMemberClassCode
INNER JOIN Student ON ClassMember.ClassMemberStudentID = Student.StudentID
INNER JOIN SENAlert ON Student.StudentID = SENAlert.SENAlertStudentID
INNER JOIN Teacher ON Class.ClassTeacherCode = Teacher.TeacherCode WHERE (Teacher.TeacherUsername = 'dsmith')

I need to extend this query by adding another table called SENAlertHistory. I would like to count the number of alerts (SENAlertID) for the specified teacherbut where there is no related record in the SENAlertHistory table. Here's what the relationship diagram looks like:

What's going to happen is when a teacher clicks a button to say they have read an alert, I will record this in the SENAlertHistory table. Therefore when performing a count of how many unread alerts the teacher has, I need to ignore the alerts they have already read (i.e. the records in the SENAlertHistory table).

This is a bit too advanced for me -- I have tried! I was hoping someone would be able to help me please?

Many thanks

Daniel


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>