0
I am with a query that is with some problem in which I cannot identify.
I need to bring up the additional sum and journey. The problem is that when I add in the bank the value of the journey and no value for the additional it comes right. From the moment I add more than 1 record in the additional it duplicates the value recorded in journey.
In the database it’s like this:
$servidores = DB::table('sch_sismapa.tb_servidor')
->join('sch_decom.tb_servidor', 'sch_sismapa.tb_servidor.nr_matricula', '=', 'sch_decom.tb_servidor.nr_matricula')
->leftJoin('sch_sismapa.tb_insalubridade','sch_sismapa.tb_servidor.id', '=', 'sch_sismapa.tb_insalubridade.id_servidor')
->leftJoin('sch_sismapa.tb_falta','sch_sismapa.tb_servidor.id', '=', 'sch_sismapa.tb_falta.id_servidor')
->leftJoin('sch_sismapa.tb_ferias','sch_sismapa.tb_servidor.id', '=', 'sch_sismapa.tb_ferias.id_servidor')
->leftJoin('sch_sismapa.tb_adicional_noturno','sch_sismapa.tb_servidor.id', '=', 'sch_sismapa.tb_adicional_noturno.id_servidor')
->leftJoin('sch_sismapa.tb_extensao_ch','sch_sismapa.tb_servidor.id', '=', 'sch_sismapa.tb_extensao_ch.id_servidor')
->select('sch_decom.tb_servidor.nm_servidor',DB::raw('COALESCE(SUM(sch_sismapa.tb_adicional_noturno.nr_adicional),0) as adicional'),
DB::raw('COALESCE(SUM(sch_sismapa.tb_extensao_ch.nr_extensao),0) as jornada'),
'sch_sismapa.tb_servidor.*',DB::raw('COUNT(sch_sismapa.tb_falta.id_servidor) as faltas'),DB::raw('COUNT(sch_sismapa.tb_ferias.id_servidor) as ferias'))
->where('sch_sismapa.tb_servidor.cd_secretaria',$request->cd_secretaria)
->where('sch_sismapa.tb_servidor.id_referencia',$request->id_referencia)
->groupBy('sch_decom.tb_servidor.nm_servidor','sch_sismapa.tb_servidor.id','sch_sismapa.tb_servidor.id_referencia','sch_sismapa.tb_servidor.nr_matricula',
'sch_sismapa.tb_servidor.cd_secretaria','sch_sismapa.tb_servidor.cd_departamento','sch_sismapa.tb_servidor.nr_dias',
'sch_sismapa.tb_servidor.st_lancado','sch_sismapa.tb_servidor.st_aprovado','sch_sismapa.tb_servidor.ds_observacao',
'sch_sismapa.tb_servidor.st_grat_reuniao')
->orderby('sch_sismapa.tb_servidor.cd_departamento', 'asc')
->orderBy('sch_decom.tb_servidor.nm_servidor', 'asc')
->skip($partes)
->take(300)
->get();
The return that this query generates is this:
It was to bring the value of 10 in journey. But it is bringing 20.
I think what’s wrong with that part:
DB::raw('COALESCE(SUM(sch_sismapa.tb_adicional_noturno.nr_adicional),0) as adicional'),
DB::raw('COALESCE(SUM(sch_sismapa.tb_extensao_ch.nr_extensao),0) as jornada'),
PS: I am using Postgresql.
SQL below:
SELECT
sch_decom.tb_servidor.nm_servidor,
COALESCE(SUM(sch_sismapa.tb_adicional_noturno.nr_adicional),
0) AS adicional,
COALESCE(SUM(sch_sismapa.tb_extensao_ch.nr_extensao),
0) AS jornada,
sch_sismapa.tb_servidor.*,
COUNT(sch_sismapa.tb_falta.id_servidor) AS faltas,
COUNT(sch_sismapa.tb_ferias.id_servidor) AS ferias
FROM
sch_sismapa.tb_servidor
INNER JOIN
sch_decom.tb_servidor ON sch_sismapa.tb_servidor.nr_matricula = sch_decom.tb_servidor.nr_matricula
LEFT JOIN
sch_sismapa.tb_insalubridade ON sch_sismapa.tb_servidor.id = sch_sismapa.tb_insalubridade.id_servidor
LEFT JOIN
sch_sismapa.tb_falta ON sch_sismapa.tb_servidor.id = sch_sismapa.tb_falta.id_servidor
LEFT JOIN
sch_sismapa.tb_ferias ON sch_sismapa.tb_servidor.id = sch_sismapa.tb_ferias.id_servidor
LEFT JOIN
sch_sismapa.tb_adicional_noturno ON sch_sismapa.tb_servidor.id = sch_sismapa.tb_adicional_noturno.id_servidor
LEFT JOIN
sch_sismapa.tb_extensao_ch ON sch_sismapa.tb_servidor.id = sch_sismapa.tb_extensao_ch.id_servidor
WHERE
sch_sismapa.tb_servidor.cd_secretaria = '7'
AND sch_sismapa.tb_servidor.id_referencia = '9'
GROUP BY sch_decom.tb_servidor.nm_servidor , sch_sismapa.tb_servidor.id , sch_sismapa.tb_servidor.id_referencia , sch_sismapa.tb_servidor.nr_matricula , sch_sismapa.tb_servidor.cd_secretaria , sch_sismapa.tb_servidor.cd_departamento , sch_sismapa.tb_servidor.nr_dias , sch_sismapa.tb_servidor.st_lancado , sch_sismapa.tb_servidor.st_aprovado , sch_sismapa.tb_servidor.ds_observacao , sch_sismapa.tb_servidor.st_grat_reuniao
ORDER BY sch_sismapa.tb_servidor.cd_departamento ASC , sch_decom.tb_servidor.nm_servidor ASC
LIMIT 300 OFFSET 0
If you run the query directly in the database correctly returns the value ?
– Bulfaitelo
Yes. It returns the value 10.
– douglas pjuizfora
so do this, update the question with an example query, and tampem does it here:
var_dump($servidores);
but change the->get()
for->toSql();
– Bulfaitelo
Dude it returns the value 20 instead of 10. I said wrong to you. I’ll put the SQL in the text for you to see.
– douglas pjuizfora
So both the eloquent and the direct sql it returns the wrong value?
– Bulfaitelo
Yes. Both are returning wrong values @Bulfaitelo
– douglas pjuizfora