Check the number of people per age

Asked

Viewed 348 times

1

How does the consultation to know the total number of people in the age range from 0 to 5 years, and if you do not have any child with a certain age, enter 0;

select idade, count(*) as qtd from pessoa group by idade order by idade

example of the current problem

+-----------+--------+
|   idade   |   qtd  |
+-----------+--------+
|     0     |    3   |
+-----------+--------+ 
|     3     |    7   |
+-----------+--------+ 
|     5     |   11   |
+-----------+--------+ 

Example of how I need the data

+-----------+--------+
|   idade   |   qtd  |
+-----------+--------+
|     0     |    3   |
+-----------+--------+ 
|     1     |    0   |
+-----------+--------+ 
|     2     |    0   |
+-----------+--------+ 
|     3     |    7   |
+-----------+--------+ 
|     4     |    0   |
+-----------+--------+ 
|     5     |   11   |
+-----------+--------+ 

2 answers

1

You can use the between to filter the age range you want.

create PROCEDURE BuscarTotalIdadePorFaixa
    @iniIdade int, 
    @fimIdade int
AS
BEGIN
    SET NOCOUNT ON;

    declare  @pessoa table(idade int, qtd  int)
    declare  @pessoaAux table(idade int, qtd  int)


    insert into @pessoa values
    (0,1),(1,1),(1,1),(1,1),(2,1),(2,1),(2,1),(2,1),(2,1),
    (4,1),(5,1),(5,1),(5,1),(6,1),(6,1),(7,1),(8,1),(8,1),(8,1),(8,1)

    declare @Idade int = @iniIdade;

    while (@Idade <= @fimIdade)
    begin
        print @Idade
        insert into @pessoaAux values (@Idade , 0); 
        set @Idade = @Idade + 1;
    end

    select idade , 0 from @pessoaAux pAux
    where not exists (select idade from @pessoa P where P.idade = pAux.idade)
    union all
    select idade, count(*) as qtd 
    from @pessoa 
    group by idade 
    having  idade between @iniIdade and @fimIdade
    order by idade

END
  • Friend, but I need you to return 0 if there are no people that age, that’s the problem.

  • see if solve, this solution was made in sql server, but the idea is the same for mysql.

0

As it is a very small set of ages that goes from zero to five years, in ORACLE has a very interesting solution for you to accomplish this with a command called merge that simply is: you have a table with zero data of Qtde, varying age from 0 to 5 and another that only has the data in fact and for the ages that do not exist, has no record (as in your case).

There may be a similar command in Mysql. I don’t know it, but it may exist.

See how interesting:

CREATE TABLE TEMP_IDADES (
  IDADE NUMBER,
  QTDE NUMBER
);

INSERT INTO TEMP_IDADES VALUES (0,0);
INSERT INTO TEMP_IDADES VALUES (1,0);
INSERT INTO TEMP_IDADES VALUES (2,0);
INSERT INTO TEMP_IDADES VALUES (3,0);
INSERT INTO TEMP_IDADES VALUES (4,0);
INSERT INTO TEMP_IDADES VALUES (5,0);

SELECT * FROM TEMP_IDADES;

-- nesta estao os dados reais
CREATE TABLE IDADES (
  IDADE NUMBER,
  QTDE NUMBER
);

INSERT INTO IDADES VALUES (4,30);
INSERT INTO IDADES VALUES (5,20);

SELECT * FROM IDADES;

-- MERGE DAS TABELAS COM AS SITUACOES ATUALIZA OU INSERE.


MERGE INTO TEMP_IDADES TEMPORARIA
   USING (SELECT IDADE, QTDE FROM IDADES) REAIS
   ON (TEMPORARIA.IDADE = REAIS.IDADE)
   WHEN MATCHED THEN 
      UPDATE SET TEMPORARIA.qtde = REAIS.qtde
   WHEN NOT MATCHED THEN 
      INSERT (TEMPORARIA.QTDE, TEMPORARIA.IDADE)
     VALUES (REAIS.QTDE,REAIS.IDADE);

SELECT * FROM TEMP_IDADES;

inserir a descrição da imagem aqui

Browser other questions tagged

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