试图找到 Mentor/mentee 对未提交特定报告的对
Trying to find Mentor/mentee pairs that have not submitted a certain report
正在尝试查找尚未提交特定报告的导师-学员组(已报告 = 147)。所有对都提交了报告,但我需要那些还没有提交 147 的人。
谢谢
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
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 and ur1.ReportID=147
我不能更具体,因为不知道您的架构。但这通常使用 GROUP BY
和 HAVING
使用条件 COUNT
来解决
GROUP BY e2.Firstname
,e2.lastname
,e1.Firstname
,e1.lastname
HAVING COUNT(CASE WHEN reported = 147 THEN 1 END) = 0
我建议:
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 ;
或者,如果您不想使用 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;
备注:
- 关键是
LEFT JOIN
确保您找到未提交报告的人。
- 不需要
Reports
table,因为ReportId
在UserReport
.
- 我对
JOIN
到 UserReport
有点怀疑。您是在暗示 "mentor" 始终提交报告。
正在尝试查找尚未提交特定报告的导师-学员组(已报告 = 147)。所有对都提交了报告,但我需要那些还没有提交 147 的人。
谢谢
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
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 and ur1.ReportID=147
我不能更具体,因为不知道您的架构。但这通常使用 GROUP BY
和 HAVING
使用条件 COUNT
GROUP BY e2.Firstname
,e2.lastname
,e1.Firstname
,e1.lastname
HAVING COUNT(CASE WHEN reported = 147 THEN 1 END) = 0
我建议:
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 ;
或者,如果您不想使用 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;
备注:
- 关键是
LEFT JOIN
确保您找到未提交报告的人。 - 不需要
Reports
table,因为ReportId
在UserReport
. - 我对
JOIN
到UserReport
有点怀疑。您是在暗示 "mentor" 始终提交报告。