3
How can I add empty values in group by
?
Example:
I have an appointment that is organized by ages:
CASE
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) < 4 THEN 'Menos de 4'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 5 AND 9 THEN '5 a 9 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 10 AND 17 THEN '10 a 17 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 18 AND 24 THEN '18 a 24 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 25 AND 29 THEN '25 a 29 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 30 AND 39 THEN '30 a 39 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 40 AND 49 THEN '40 a 49 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 50 AND 59 THEN '50 a 59 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 60 AND 69 THEN '60 a 69 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) >= 70 THEN 'Maior que 70 Anos'
ELSE 'SEM INFORMAÇÕES' END
But when the values have no results they do not appear in select, I wanted to add 0 in all these options from CASE
.
My select all for reference:
SELECT CASE
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) < 4 THEN 'Menos de 4'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 5 AND 9 THEN '5 a 9 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 10 AND 17 THEN '10 a 17 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 18 AND 24 THEN '18 a 24 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 25 AND 29 THEN '25 a 29 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 30 AND 39 THEN '30 a 39 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 40 AND 49 THEN '40 a 49 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 50 AND 59 THEN '50 a 59 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) BETWEEN 60 AND 69 THEN '60 a 69 Anos'
WHEN TIMESTAMPDIFF(YEAR, STR_TO_DATE(DataNascimento, '%d/%m/%Y'),CURDATE()) >= 70 THEN 'Maior que 70 Anos'
ELSE 'SEM INFORMAÇÕES' END ,
COUNT(id)
FROM pessoas GROUP BY DataNascimento
It helped me a lot was exactly what I was looking for, I forgot about the coalesce, let me ask you and in the case of the case, how can I use the coalesce?
– Guilherme Freire
I think there is something wrong because I use Count(id) and it does not count as 0 the values that do not appear in the query
– Guilherme Freire
In fact everything he does not find will be set by the coalition as 0. Following the logic will never fall into the Else, for the first
WHEN
is< 4
. If you still fall inELSE
it would be interesting to check record by records that are falling in this condition– DNick
I have records with 99/99/9999, so I need that when it has no value < 4 for example it fill 0 and is not doing it
– Guilherme Freire
The correct thing would be to treat this information via bank change, because, as you well know there is no such date. Change these values to
null
.– DNick
my biggest problem is not this, but when it has no value it does not put as 0
– Guilherme Freire
Still remains data formatting problem in the database. At the time of saving you need to have a treatment, a pattern, to apply when the data to be saved come empty. It still remains the case to set as
null
.– DNick
Even though I set as null still not showing 0 when it does not have value
– Guilherme Freire
I took the test in the bank and it worked. I added the image in the reply to see. If you have changed the value no, the database will understand as a null string not a null field. Apply the change via the UPDATE command and set the value to null.
– DNick
My problem is that this null does not come, I want to fill in the values that do not come with these options EX: table persons id 1, Datanascimento 10/10/2015 id 2, Datanascimento 20/02/2000 not going to appear the other options only <4 and 10 to 17 Years I want to show all these options and the cont(id) 0
– Guilherme Freire
The answer does not address the @Guilhermefreire problem. Using COALESCE will not produce the desired result.
– bruno