Sum of values in Mysql

Asked

Viewed 28 times

0

Good evening. I have to add the natures according to each sector of the company, but it is not going with Count.

SQL:

SELECT examefuncionario.exameFunc_Natureza, setor.setor_Nome
FROM `funcionario` 
JOIN examefuncionario ON examefuncionario.exameFunc_CodFuncionario = funcionario.CodFuncionario
JOIN setor on setor.CodSetor = funcionario.funcionario_CodSetor
WHERE `funcionario_CodEmpresa` = '274'

Foto do resultado do sql

I wanted it to stay that way for example:

A    D    P   Setor
2    1    5   Portaria/Limpeza
5    2    1   Montagem

1 answer

2


Use the GROUP BY together with a COUNT conditional.

SELECT COUNT(CASE examefuncionario.examefunc_natureza WHEN 'A' THEN 1 ELSE NULL END) AS A,
       COUNT(CASE examefuncionario.examefunc_natureza WHEN 'D' THEN 1 ELSE NULL END) AS D,
       COUNT(CASE examefuncionario.examefunc_natureza WHEN 'P' THEN 1 ELSE NULL END) AS P, 
       setor.setor_nome 
  FROM `funcionario` 
  JOIN examefuncionario 
    ON examefuncionario.examefunc_codfuncionario = funcionario.codfuncionario 
  JOIN setor ON setor.codsetor = funcionario.funcionario_codsetor 
 WHERE `funcionario_codempresa` = '274'
 GROUP BY setor.setor_nome
  • 1

    Thank you very much. It worked right! Hugs, good night

Browser other questions tagged

You are not signed in. Login or sign up in order to post.