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 x@x.com test test 2012-05-11 11:00
54 b@b.com test2 test2 2012-05-10 10:00
55 h@h.com blah blah 2012-05-11 12:00
56 t@t.com 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.:
- mail 54 was first hidden for TEAM02 – should be invisible for them
- mail 54 was assigned to TEAM01 – should be now visible for them in “This Team’s” view
- mail 54 was processed by TEAM01 – should be now invisible in “This Team’s” view
- mail 53 was assigned to TEAM01 – should visible in their “This Team’s” view (and ONLY for them)
- mail 53 was hidden for TEAM01 – should be invisible to TEAM01 (and for anyone else because of prev. action)
- mail 56 was assigned to TEAM01 – should be now visible only to TEAM01 in “This Team’s”
- mail 56 was processed by TEAM01 – should be now invisible for TEAM01 in “This Team’s”
So, with the present data in both tables, finally, TEAM01 should see in their “Unprocessed” view only:
mail 55
Would this do what you want?