select contract.id, invoice.period_to
from public.invoice
right join public.contract on invoice.contract_id = contract.id
where (MAX(invoice.period_to) <= '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;
I know i cant use MAX under WHERE so im try doing something like
where (invoice.period_to IN(SELECT MAX(invoice.period_to) FROM public.invoice GROUP BY invoice.period_to ) <= '2017-10-01' OR invoice.id IS NULL)
But error
ERROR: invalid value for logical type: "2017-10-01"
Help me pls