Summarizing birthday girls by month (SQL)

Asked

Viewed 74 times

0

I’m trying to count the number of birthday clients per month in my database, but I can only make it display the months that have birthday kids, for example:

inserir a descrição da imagem aqui

But I need him to show the months that there are no birthday girls on the count of 0, but I can’t do it.

Code I made so far

 --Aniversariante por Mês (quantidade e cada mês)
go
create procedure Aniversariante_Count
as
    begin

        select case MONTH(C.DATANASCIMENTO)
        when 1  then 'Janeiro'
        when 2  then 'Fevereiro'
        when 3  then 'Março'
        when 4  then 'Abril'
        when 5  then 'Maio'
        when 6  then 'Junho'
        when 7  then 'Julho'
        when 8  then 'Agosto'
        when 9  then 'Setembro'
        when 10 then 'Outubro'
        when 11 then 'Novembro'
        when 12 then 'Dezembro'
        end as MES,  count(month(C.DATANASCIMENTO)) as Aniversariantes
        from CLIENTES C 
        group by month(C.DATANASCIMENTO) 
    
    end

1 answer

0

Good morning Bea!

To bring the fields in a Count considering the nulls and giving the value zeroed if I have nothing there is the function COALESCE, because I believe it will be able to help you.

obs. The COALSESCE did not work because the problem is not that the months have null values, there is no Month within the base and so it does not can really bring, in which case the best is to create a structure and so insert the values in, follows how to do:

Here we create the table with empty fields to make the left Join in your table and even if there are no values it will work:

CREATE TABLE meses_novo (id INTEGER PRIMARY KEY, meses TEXT);

INSERT INTO meses VALUES (1, "Janeiro");
INSERT INTO meses VALUES (2, "Fevereiro");
INSERT INTO meses VALUES (3, "Março");
INSERT INTO meses VALUES (4, "Abril");
INSERT INTO meses VALUES (5, "Maio");
INSERT INTO meses VALUES (6, "Junho");
INSERT INTO meses VALUES (7, "Julho");
INSERT INTO meses VALUES (8, "Agosto");
INSERT INTO meses VALUES (9, "Setembro");
INSERT INTO meses VALUES (10, "Outubro");
INSERT INTO meses VALUES (11, "Novembro");
INSERT INTO meses VALUES (12, "Dezembro");

SELECT * FROM meses_novo;

I’ll keep the old QUERY so you don’t get confused:

--Aniversariante por Mês (quantidade e cada mês)
go
create procedure Aniversariante_Count
as
    begin

        select case MONTH(C.DATANASCIMENTO)
        when 1  then 'Janeiro'
        when 2  then 'Fevereiro'
        when 3  then 'Março'
        when 4  then 'Abril'
        when 5  then 'Maio'
        when 6  then 'Junho'
        when 7  then 'Julho'
        when 8  then 'Agosto'
        when 9  then 'Setembro'
        when 10 then 'Outubro'
        when 11 then 'Novembro'
        when 12 then 'Dezembro'
        end as MES, count(month(C.DATANASCIMENTO)) as Aniversariantes
        from CLIENTES C 
        group by month(C.DATANASCIMENTO) 
    
    end

Last QUERY doing Join between the 2 created tables:

 CREATE TABLE uniao as select
  a.*,
  b.Aniversariantes
 
  FROM a.meses_novo a left join Aniversariante_Count b on
  a.meses = b.MES
  END
  • Thank you for the reply, but I tested here and gave anyway, still does not appear months without birthday

  • 1

    Bea if it did not work out is because as you are creating the months using the "CASE WHEN" will not appear even with the COALENSCE, in which case the best way would be using a left Join over a base of the months.

  • I tried it here and it worked, thank you very much

Browser other questions tagged

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