Error using Join and Count

Asked

Viewed 48 times

0

Hello, the error now is that it is adding the Codsector. The values would be 6 and 3, but is coming 6 and 6. Follow photos and codes.

SELECT *, COUNT(`funcionario`.`funcionario_CodSetor`) AS total 
FROM `funcionario` 
    JOIN `setor` ON `setor`.`CodSetor` = `funcionario`.`funcionario_CodSetor` 
    JOIN `horario` ON `horario`.`horario_CodSetor` = `funcionario`.`funcionario_CodSetor` 
    JOIN `cbo` ON `cbo`.`CodCBO` = `funcionario`.`funcionario_CodCBO` 
    JOIN `familiacbo` ON `familiacbo`.`CodFamilia` = `cbo`.`cbo_CodFamilia`
WHERE `funcionario_CodEmpresa` = '32' 
GROUP BY `funcionario`.`funcionario_CodSetor`

Without giving the Join in schedule of the sector becomes normal

Puxando o total com horario

Puxando o total sem horario

  • Are you grouping by the field you are counting? You are supposed to group by everyone else except for this. Try to change the COUNT for this: COUNT(DISTINCT funcionario.funcionario.Cod_Setor).

  • Using Distinct returns only the value 1 in the two fields.

No answers

Browser other questions tagged

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