Angus Thermopily
Angus Thermopily

Reputation: 13

SQL Multiple Where clauses or Select Case? Or something else?

I want to search the Incidents table and get a listing of all tickets that are not closed, resolved or cancelled and that belong to either Voice & Data Management or Voice & Data Phone Techs. The code below does the job but is there a better way to do it? Do I have to keep adding the Status NOT In every time I add a group or is there something like Select Case or something else I should be using?

SELECT OwnerTeam, Status, Owner
FROM Incident
WHERE Status NOT IN ('Closed', 'Cancelled', 'Resolved')
  AND OwnerTeam = 'Voice & Data Management' 
  or Status NOT IN ('Closed', 'Cancelled', 'Resolved') 
  and OwnerTeam = 'Voice & Data Phone Techs'  

Upvotes: 0

Views: 40

Answers (1)

Kurt
Kurt

Reputation: 1748

Combining AND and OR clauses in a long chain will not always do what you expect it to do. To be safe, you want to use parentheses to specifically group related expressions, i.e. "A and B or C and D" is not semantically identical to "(A and B) or (C and D)".

In this case, just combine your OwnerTeam conditions into an IN clause:

WHERE Status NOT IN ('Closed', 'Cancelled', 'Resolved')
  AND OwnerTeam IN ('Voice & Data Management', 'Voice & Data Phone Techs')

Upvotes: 2

Related Questions