I have 5 tables I want to join
Invoices Coletes Clients TagaCars Incasaris
id 1 1 1 1 1
2
3
My query is: summing all the child coletes sum where coletes.invoice_id = invoices.id selecting the clients name selecting the tagacars model BUT is supposed to sum all the incasaris(which is basically encashment of an invoice), but when I do the sum in query it is doubling the sum for no reason
This is my query:
$invoices = Invoice::join('clients','clients.id','=','invoices.client_id')
->join('taga_cars','taga_cars.id','=','invoices.car_id')
->leftJoin('coletes','coletes.invoice_id','=','invoices.id')
->leftjoin('incasaris','invoices.id','=','i_plati_id')
->selectRaw('
sum(coletes.totaleuro) as total,
sum(coletes.totaleuro * invoices.i_rate) as totalabsolut,
sum(incasaris.i_totallei) as totalincasat,
clients.name, invoices.*, taga_cars.model')
->groupBy('invoices.id')
->orderBy('invoices.id')
->get();
The problem is only with "totalincasat"
Here you can see in my sql the ammount of i_totallei for one of my invoices
and here is my DD which is showing the doubled ammount for no reason
I want to mention that at the moment each invoice has only one incasaris belonging to each, and also, all incasaris summed combined would not be summed as 2000, it is simply doubling the result Does anyone have any suggestion? Is my first time I got to this "error" and I don't seem to understand how to fix it.
Please check leftjoin of invices with incasaris, you are missing incasaris.i_plati_id in bellow mentioned line:
->leftjoin('incasaris','invoices.id','=','i_plati_id')
Instead of above, it must be like
->leftjoin('incasaris','invoices.id','=','incasaris.i_plati_id')