Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
Laravel9 - selectRaw sum doubling the result error

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.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

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')
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda