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;
Ich weiß, ich MAX unter Verwendung kippe WHERE so im versuchen, so etwas wieSQL MAX in WHERE
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)
Aber Fehler ERROR: invalid value for logical type: "2017-10-01"
mich tun Hilfe pls
Aggregatfilter sollten in eine 'have'-Klausel gestellt werden, nicht in die' where'-Klausel. – Igor
HABEN ist was du suchst. –
Mögliches Duplikat von [Sql Server: Verwendung einer Aggregatfunktion wie MAX in einer WHERE-Klausel] (https://stackoverflow.com/questions/1475589/sql-server-how-to-use-an-aggregate-function- like-max-in-a-where-Klausel) –