'SQL - WHERE clause with two different sets of conditions?
I'm using Microsoft SQL management studio on SQL server 2016. I am trying to filter out some results from two separate sets of conditions. The first set does the heavy work, but I just need to add a second set of conditions that filter out just a few rows that actually are not filtered out by the first set of conditions. These other rows that need to be filtered have to meet the exact combination of extra criteria or otherwise too many rows will be filtered.
So I have this:
SELECT
postdate
,costarea
,storeloc
,user
,movetype
,account
FROM table1
WHERE
--Condition 1
(postdate > '12/31/2015'
AND costarea <> 'None'
AND storeloc <> '50A')
OR
--Condition 2
(account <> '5500'
AND user <> 'JDOE'
AND movetype <> '405')
Basically, I need to filter the rows under condition 1 and then filter the few rows that would be filtered by condition 2 but are not filtered by condition 1 as they pass those conditions. What would be the best way to go about this?
Solution 1:[1]
I think what you are trying for is...
where
NOT (
condition1
or
condition2
)
But you would have to reverse the logic some by doing
where
NOT
(
( postdate < '2016-01-01'
AND costarea = 'None'
AND storeloc = '50A' )
OR
( account = '5500'
AND user = 'JDOE'
AND movetype = '405')
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DRapp |
