WHAT I'M DOING:
I'm writing a C# application which will help employees process emails. I have a database with mails (SQL Server 2008 Express). One table contains the mails themselves (each row has columns like msgId, sender, recipients, subject, body etc) and the other table contains data of how the mails have been processed by employees - whenever an email has some property assigned, a new row with the property is saved to the second table.
The second table contains columns: msgId, forTeam, notForTeam, processedByTeam. Whenever an employee marks a message as related to his team, a row is saved with TEAMXX value in the forTeam column. Whenever an employee performs other actions on the email, a row with TEAMXX value is saved in the processedByTeam column.
I'm trying to get emails to display in the "Unprocessed" view, ie. mails which have following values:
forTeam: null
notForTeam: everything different than 'TEAM01'
processedByTeam: everything different than 'TEAM01'
I've been trying to write many different queries but it still doesn't work the way I want and it's driving me nuts. Will be grateful for the correct query.
EXAMPLE:
Table with mails (Mails):
msgId sender subject body received (sortingTime)
53 [email protected] test test 2012-05-11 11:00
54 [email protected] test2 test2 2012-05-10 10:00
55 [email protected] blah blah 2012-05-11 12:00
56 [email protected] dfgg dfgg 2012-05-11 12:30
Table with assignments (MailAssignments):
msgId forTeam notForTeam processedByTeam
54 null TEAM02 null - this means TEAM02 hid the mail from their view
54 TEAM01 null null - this means TEAM01 assigned the mail to them
54 null null TEAM01 - TEAM01 has processed the mail
53 TEAM01 null null - invisible to other teams
53 null TEAM01 null - invisible for TEAM01 (and also for others because of the above)
56 TEAM01 null null
56 null null TEAM01 - mail processed by TEAM01
Now, I want my C# app to have a view which will display mails as "Unprocessed", ie.:
So, with the present data in both tables, finally, TEAM01 should see in their "Unprocessed" view only:
mail 55
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
)
Would this do what you want?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With