Group data by age

0

Well I have a database that contains professions, people, and sex

I wanted to group by age group and by profession and sex and make a calculation of them.

problem repeats several bands for example the profession has four lines of F sex, with the same profession and different count values for the same age range.

  SELECT   SEXO.SEXO,
           PROFISSAO.PROFISSAO,
           COUNT (CASE WHEN DADOS.IDADE BETWEEN "0" AND "20" THEN "ate 20" END)
              AS FAIXAETARIA20,
           COUNT (
              CASE WHEN DADOS.IDADE BETWEEN "21" AND "50" THEN "de 21 a 50" END
           )
              AS FAIXAETARIA2250,
           COUNT (
              CASE WHEN DADOS.IDADE BETWEEN "51" AND "75" THEN "de 51 a 75" END
           )
              AS FAIXAETARIA51200
    FROM         DADOS
              LEFT JOIN
                 SEXO
              ON (DADOS.SEXO = SEXO.ID)
           LEFT JOIN
              PROFISSAO
           ON (DADOS.PROFISSAO = PROFISSAO.ID)
GROUP BY   PROFISSAO.PROFISSAO, SEXO.SEXO, DADOS.IDADE
    
asked by anonymous 15.02.2016 / 13:57

1 answer

0

Colleague.

Your query is bringing more results because you are grouping by IDADE as well. Just remove it from the query, grouping it only by SEXO and PROFISSAO (GROUP BY).

Follows:

  SELECT   SEXO.SEXO,
           PROFISSAO.PROFISSAO,
           COUNT (CASE WHEN DADOS.IDADE BETWEEN 0 AND 20 THEN 'ate 20' END)
              AS FAIXAETARIA20,
           COUNT (
              CASE WHEN DADOS.IDADE BETWEEN 21 AND 50 THEN 'de 21 a 50' END
           )
              AS FAIXAETARIA2250,
           COUNT (
              CASE WHEN DADOS.IDADE BETWEEN 51 AND 75 THEN 'de 51 a 75' END
           )
              AS FAIXAETARIA51200
    FROM         DADOS
              LEFT JOIN
                 SEXO
              ON (DADOS.SEXO = SEXO.ID)
           LEFT JOIN
              PROFISSAO
           ON (DADOS.PROFISSAO = PROFISSAO.ID)
GROUP BY   PROFISSAO.PROFISSAO, SEXO.SEXO;
    
15.02.2016 / 15:09