Search code examples
sqlarcgis

SQL - AND OR combination


This query works

SELECT *
FROM ALL
WHERE Date >= '2017-04-04 00:00:00'
AND
CONFIDENCE <> 'Discarded'
AND 
CONTEXT <>'Home'

I want to be able to say: And (CONTEXT <> 'Home') OR (CONTEXT = 'Hospital' AND LOCAL = 1)

So that I can select all records Either from April 2017 onwards, not discarded, that have a context not equal to home

OR

From April 2017 onwards, not discarded, OR with a context = hospital and local = 1.

I'm using ArcGIS. I have tried this:

"DATE_ENTER"  >= date '2017-04-04 00:00:00'
AND
"CONFIDENCE" <> 'Discarded'
AND 
(
("CONTEXT" <> 'Home address') OR ("CONTEXT" = 'Hospital' AND "LOCAL" = 1)
)

But there are still records with Context = Hospital and Local = 0

When I test

 ("CONTEXT" <> 'Home address') 

or

("CONTEXT" = 'Hospital' AND "LOCAL" = 1)

alone, it works, I'm having trouble combining the two to give me what I need.

I had a look at this SQL AND OR query

first.


Solution

  • WHERE Date >= '2017-04-04' AND  -- time component is unnecessary
      CONFIDENCE <> 'Discarded' AND 
      CONTEXT <> 'Home' AND NOT(CONTEXT = 'Hospital' AND LOCAL = 0) 
    

    Works

    as does

        WHERE Date >= '2017-04-04' AND  -- time component is unnecessary
                  CONFIDENCE <> 'Discarded' 
    AND
        (
           ("CONTEXT" <> 'Home address' AND "CONTEXT" <> 'Hospital') 
           OR ("CONTEXT" = 'Hospital' AND "LOCAL" = 1)
        )