I have an order table,
Invoice Location Customer Code SalesPersonEmail
------------------------------------------------------
300001 001 CUS001 ?
300002 006 CUS002 ?
And the email group table,
Role Email
-----------------------------------------------------
Filtered_Group Management@gmail.com;john@gmail.com
When Location = 001, SalesPersonEmail must be an email field from Filtered_Group
SalesPersonEmail for all other locations must be " Orders@gmail.com ;" + Email for the No_Filter_Group role.
I am currently using the following for this:
SELECT i.Invoice, i.Location, i.[Customer Code],
CASE WHEN i.Location = 001
THEN f.Email
ELSE N'Orders@gmail.com;' + nf.Email as SalesPersonEmail
END
FROM Invoice i, RoleCodes f, RoleCodes nf
WHERE f.Role = N'Filtered_Group' AND nf.Role = N'No_Filter_Group'
My problem is that the role of No_Filter_Group sometimes cannot exist in the role table, which leads to the fact that the above query returns nothing.
How can I join these tables correctly, so if No_Filter_Group does not exist in the table, rows that have SalesPersonEmail from Filtered_Group are still returned from the query?
thank