DECLARE @teamName varchar(50) = 'TEAM01'
SELECT *
FROM Mails M
LEFT OUTER JOIN MailAssignments MA ON M.msgId = MA.msgId
WHERE MA.msgId IS NULL OR
(
MA.forTeam IS NULL AND
MA.notForTeam <> @teamName AND
MA.processedByTeam <> @teamName
)
Apakah ini akan melakukan apa yang Anda inginkan?