2
I have a billing chart. The significant fields are as follows:
| vencimento | data_pagamento | valor_pago |
I need to make an appointment to call me back:
- The number of unpaid arrears;
- The number of instalments paid;
- The number of unpaid instalments which have not yet expired;
I’m trying this way:
SELECT
COUNT(CASE WHEN vencimento < NOW() AND data_pagamento IS NULL THEN 1 ELSE 0 END) as 'VENCIDAS',
COUNT(CASE WHEN data_pagamento IS NOT NULL THEN 1 ELSE 0 END) as 'PAGAS',
COUNT(CASE WHEN vencimento >= NOW() AND data_pagamento IS NULL THEN 1 ELSE 0 END) as 'A VENCER'
FROM boletos WHERE cliente = 12345;
But it does not return me the correct values.
Is there any better way ?
What I’m doing wrong using CASE WHEN
?
It is necessary this
group by
ororder by
by filtering only by one customer ?– Pedro Augusto
The
ORDER BY
only makes sense if you don’t filter through a customer. TheGROUP BY
must always exist because of the columncliente
who is in theSELECT
, but if you remove it from the query you can also remove theGROUP BY
.– João Martins