Somar valor de três colunas

2 respostas
mysqlsql
bruno_i25

Não estou conseguindo somar as tres colunas fin.

select ID_GARANTIA,count(ia.ID_GAR)AS finAut,count(lb.ID_GAR)AS finLb,count(inf.ID_GAR)AS finInf from garantias as g 
left join itens_automotivo as ia on g.ID_GARANTIA=ia.ID_GAR
left join itens_linhabranca as lb on g.ID_GARANTIA=lb.ID_GAR
left join itens_informatica as inf on g.ID_GARANTIA=inf.ID_GAR where g.ID_GARANTIA and ia.LAUDO=1 or lb.LAUDO=1 or inf.LAUDO=1 
group by g.ID_GARANTIA order by g.ID_GARANTIA;

image

Tentei fazer isto mas dá o seguinte erro:

select ID_GARANTIA,sum(count(ia.ID_GAR)+count(lb.ID_GAR)+count(inf.ID_GAR))as finalizadas from garantias as g 
left join itens_automotivo as ia on g.ID_GARANTIA=ia.ID_GAR
left join itens_linhabranca as lb on g.ID_GARANTIA=lb.ID_GAR
left join itens_informatica as inf on g.ID_GARANTIA=inf.ID_GAR where g.ID_GARANTIA and ia.LAUDO=1 or lb.LAUDO=1 or inf.LAUDO=1 
group by g.ID_GARANTIA order by g.ID_GARANTIA;

Error Code: 1111.Invalid use of group function

2 Respostas

V
V

Acho que a função sum é desnecessária no seu código

(count(ia.ID_GAR)+count(lb.ID_GAR)+count(inf.ID_GAR))as finalizadas

Não é porque você esta fazendo uma soma que precisa usar o sum.

Criado 29 de março de 2018
Ultima resposta 29 de mar. de 2018
Respostas 2
Participantes 2