Trying to find the mentor-mentee groups that have not submitted a specific report (reported = 147). All the pairs have submitted reports but I need the ones who have specifically not submitted 147 yet.
Thanks
SELECT DISTINCT
e2.Firstname Mentor_FN
,e2.lastname Mentor_LN
,e1.Firstname Mentee_FN
,e1.lastname Mentee_LN
,r.ReportID
FROM
MentorRelationshipStaging m
INNER JOIN Employee e1
ON e1.EmployeeCode = m.MenteeCode
INNER JOIN Employee e2
ON e2.EmployeeCode = m.MentorCode
INNER JOIN UserReport ur1
ON ur1.EmployeeID = e2.EmployeeID
INNER JOIN Report r
ON r.reportID = ur1.ReportID
DATABASE:https://i.sstatic.net/qphEq.jpg
I would suggest:
SELECT e2.Firstname Mentor_FN, e2.lastname Mentor_LN, e1.Firstname Mentee_FN, e1.lastname Mentee_LN
FROM MentorRelationshipStaging m INNER JOIN
Employee e1
ON e1.EmployeeCode = m.MenteeCode INNER JOIN
Employee e2
ON e2.EmployeeCode = m.MentorCode LEFT JOIN
UserReport ur1
ON ur.EmployeeID = e2.EmployeeID
GROUP BY e2.Firstname Mentor_FN, e2.lastname Mentor_LN, e1.Firstname Mentee_FN, e1.lastname Mentee_LN
HAVING SUM(CASE WHEN ur.ReportId = 147 THEN 1 ELSE 0 END) = 0 ;
Or, if you don't want to use GROUP BY
:
SELECT e2.Firstname Mentor_FN, e2.lastname Mentor_LN, e1.Firstname Mentee_FN, e1.lastname Mentee_LN
FROM MentorRelationshipStaging m INNER JOIN
Employee e1
ON e1.EmployeeCode = m.MenteeCode INNER JOIN
Employee e2
ON e2.EmployeeCode = m.MentorCode LEFT JOIN
UserReport ur1
ON ur.EmployeeID = e2.EmployeeID AND ur.ReportId = 147
WHERE ur.EmployeeId IS NULL;
Notes:
LEFT JOIN
to be sure you get people who have submitted no reports.Reports
table is not needed, because the ReportId
is in UserReport
.JOIN
to UserReport
. You are suggesting that the "mentor" is always submitting the report.