how are they? I would need to get the following amounts:
Show for each contract total amount of commissions, amount to pay, amount to paid.
The tables are the following:
Commissions: contract_number (PK-FK of contracts), payment_date, commission_amount Contracts: contract_number (PK), contract_date.
I made the following query:
select cont.nro_contrato, count(*), if(com.fecha_pago is not null,
count(com.fecha_pago) , 0) as pagadas,
if(com.fecha_pago is null, count(*), 0) as a_pagar
from contratos as cont inner join comisiones as com
on cont.nro_contrato = com.nro_contrato
group by cont.nro_contrato;
The result is as follows:
But if I ask for the commissions, effectively in contract n° 1 for example, there is an empty payment_date, so it would have to count in payable, and not show 0 as it does:
Could someone give me a hand? Thank you very much!
What you are looking for you have to solve inside the function
count()
, not outside, something like this:It's basically a conditional count, the condition is evaluated inside the aggregation function using a
case
.