title: Postgres: negating conditions in CASE statement
body: <p>I have a list of rows I want to filter out from my result set based on some conditions. Currently, it looks like this in my query:</p>
  
  <pre><code>WHERE col1 = 'x' OR col1 ~~ '%y%' AND col2 = 'z' ...
  </code></pre>
  
  <p>Now I want to introduce a new column that documents the reason certain rows are filtered out:</p>
  
  <pre><code>SELECT ...
    , CASE WHEN condition1 THEN 'Exclusion Reason 1'
           WHEN condition2 THEN 'Exclusion Reason 2'
           ...
           ELSE ''
      END AS exclusion_reason
  </code></pre>
  
  <p>The problem: it doesn't seem to work the way I use negation:</p>
  
  <pre><code>SELECT ...
    , CASE WHEN condition1 THEN 'Reason 1'
           WHEN condition2 THEN 'Reason 2'
           WHEN NOT (col1 = 'x' OR col1 ~~ '%y%' AND col2 = 'z'...) THEN 'Reason 3'
           ELSE ''
      END AS exclusion_reason
  </code></pre>
  
  <p>It doesn't return any rows with <code>Reason 3</code>, although it should. Leaving out the NOT marks a subset of the rows:</p>
  
  <pre><code>SELECT ...
    , CASE WHEN condition1 THEN 'Reason 1'
           WHEN condition2 THEN 'Reason 2'
           WHEN (col1 = 'x' OR col1 ~~ '%y%' AND col2 = 'z'...) THEN 'Not Reason 3'
           ELSE ''
      END AS exclusion_reason
  </code></pre>
  
  <p>Why does the negation not work as expected and what is the correct syntax to use here?</p>