Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get unmarked data from db

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.:

  1. mail 54 was first hidden for TEAM02 - should be invisible for them
  2. mail 54 was assigned to TEAM01 - should be now visible for them in "This Team's" view
  3. mail 54 was processed by TEAM01 - should be now invisible in "This Team's" view
  4. mail 53 was assigned to TEAM01 - should visible in their "This Team's" view (and ONLY for them)
  5. mail 53 was hidden for TEAM01 - should be invisible to TEAM01 (and for anyone else because of prev. action)
  6. mail 56 was assigned to TEAM01 - should be now visible only to TEAM01 in "This Team's"
  7. 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
like image 260
Val Avatar asked May 08 '26 01:05

Val


1 Answers

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?

like image 148
Jakub Konecki Avatar answered May 09 '26 15:05

Jakub Konecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!