Grouping data by age

Asked

Viewed 401 times

0

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

wanted to group by age group and by profession and sex and make a calculation of the same.

problem repeats me several tracks for example the troll profession has 4 lines of sex F , with the same profession and different counting 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
  • Make a calculation of them, what do I calculate? involving what?

  • To group the way you said it is only to do GROUP BY 3,2,1.

1 answer

0


Fellow.

Your query is bringing more results because you are grouping by IDADE also. Simply 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;

Browser other questions tagged

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