I have the following SQL:
select
    `sites`.`number`,
    SUM(invoices.amount),
    SUM(payment_details.amount) as pda,
    COUNT(warrels.id) as warrels
from `sites`
left join `invoices`
    on `sites`.`id` = `invoices`.`invoiceable_id`
left join `warrels`
    on `sites`.`id` = `warrels`.`site_id`
left join `payment_details`
    on `invoices`.`id` = `payment_details`.`payable_id`
where `invoices`.`invoiceable_type` = 'App\\Models\\Site'
  and `sites`.`number` LIKE 'AU22%'
group by `sites`.`number`
order by `sites`.`number` asc
In most cases this returns correctly calculated sums. I calculate the overall balance in a simplified manner.
But when there are multiple payment_details associated with an invoice the SUM of the invoices.amount adds a duplicate to the overall sum. If I remove the join on payment_details, the sum of the invoices.amount always returns correctly.
 
     
    