title: SQL MAX in WHERE
body: <pre><code>select contract.id, invoice.period_to
  from public.invoice
  right join public.contract on invoice.contract_id = contract.id
  where (MAX(invoice.period_to) &lt;= '2017-10-01' OR invoice.id IS NULL)
         ^^^^^^^^^^^^^^^^^^^^^ - I need help here
  AND contract.close_type IS NULL 
      AND contract.payment_type != 'TRIAL' 
      group by contract.id;
  </code></pre>
  
  <p>I know i cant use MAX under WHERE so im try doing something like </p>
  
  <pre><code>where (invoice.period_to IN(SELECT MAX(invoice.period_to) FROM public.invoice GROUP BY invoice.period_to ) &lt;= '2017-10-01' OR invoice.id IS NULL)
  </code></pre>
  
  <p>But error 
  <code>ERROR: invalid value for logical type: "2017-10-01"</code></p>
  
  <p>Help me pls</p>