2
I’m trying to use the function coalesce to return 0 when my query does not have a return value, but I have tried a thousand and one ways and I cannot return 0 at all. I want to know where I’m going wrong:
select
case
when date_part('month', p.data_cadastro) = 1 then '01.Jan'
when date_part('month', p.data_cadastro) = 2 then '02.Fev'
when date_part('month', p.data_cadastro) = 3 then '03.Mar'
when date_part('month', p.data_cadastro) = 4 then '04.Abr'
when date_part('month', p.data_cadastro) = 5 then '05.Mai'
when date_part('month', p.data_cadastro) = 6 then '06.Jun'
when date_part('month', p.data_cadastro) = 7 then '07.Jul'
when date_part('month', p.data_cadastro) = 8 then '08.Ago'
when date_part('month', p.data_cadastro) = 9 then '09.Set'
when date_part('month', p.data_cadastro) = 10 then '10.Out'
when date_part('month', p.data_cadastro) = 11 then '11.Nov'
else '12.Dez'
end as mes,
coalesce(count(*), '0'),
s.descricao as situacao
from
tb_historico p
inner join
tb_situacao s on s.id_situacao = p.id_situacao
where
date_part('year', p.data_cadastro) = 2015
and s.descricao = 'APROVADO'
and id_usuario = 10
and id_tipo_projeto = 1
group by mes, s.descricao
order by mes
The Coalesce returns for the field of projection the value passed in the second parameter if the first parameter is null, when there is no return, there is no projection field and therefore will not return anything. What is the need to return the field to zero?
– Caputo
I want the following to appear... In the case of counting the tuples from zero to some month, currently it does not appear that line, but I want it to appear. Let’s say that in the month of January there was no line, I want it to appear 01.Jan | 0
– Lucas Vasconcelos
a simple gambiarra would you create a table with the 12 months and make a left Join...
– iuristona
Yes @iuristona hehe I had already thought about it, but my system and banks are already in production. I wanted to do something way without the gambiarras.
– Lucas Vasconcelos
Tried to change
inner join
forleft join
?– abfurlan