1

Tenho uma consulta abaixo que os registros que não tem nada está vindo com 0. Queria que pudesse vir em branco. Exemplo:

select 
F.C37 [Pontuação],
ISNULL(count(distinct TD.UsuIDCliente),'-')   [Qtde_Clientes Detratores],
ISNULL(count(distinct TN.UsuIDCliente),'-')    [Qtde_Clientes Neutro],
ISNULL(count(distinct TP.UsuIDCliente),'-')    [Qtde_Clientes Promotores]
from FRM_8 F
left join Tarefa TD on TD.TarID = F.TarefaID and F.C37 between 0 and 5 AND TD.TarFechamento between '01-07-2017' and '17-08-2017' -- Detratores
left join Tarefa TN on TN.TarID = F.TarefaID and F.C37 between 6 and 8 AND TN.TarFechamento between '01-07-2017' and '17-08-2017' --Neutro
left join Tarefa TP on TP.TarID = F.TarefaID and F.C37 between 9 and 10 AND TP.TarFechamento between '01-07-2017' and '17-08-2017' --Promotores
where 
F.C38 is not null and F.C37 is not null
group by F.C37
order by  
CASE F.C37 WHEN '1' THEN 0 
                    WHEN '2' THEN 1
                    WHEN '3' THEN 2
                    WHEN '4' THEN 3
                    WHEN '5' THEN 4
                    WHEN '6' THEN 5
                    WHEN '7' THEN 6
                    WHEN '8' THEN 7
                    WHEN '9' THEN 8
                    WHEN '10' THEN 9
END

Hoje está vindo dessa maneira:

Pontuacao      Clientes Detratores    Clientes Neutros
4                    0                      2
5                    1                      5
6                    3                      0

Eu quero que em vez de vir 0, venha NULL ou em branco.

2
  • como assim "está vindo com zero"?
    – rLinhares
    17/08/2017 às 20:35
  • ôpa @rLinhares, editei a pergunta. Abraços 17/08/2017 às 20:38

2 Respostas 2

2

O problema é que o tipo de retorno esperado na coluna é int.

Você pode retornar null ou converter a coluna para varchar. Segue código de exemplo:

select
c.id,
count(distinct t1.id) as chave1,
count(distinct t2.id) as chave2,
(case count(distinct t3.id) when 0 then null else count(distinct t3.id) end)  as chave3,
(case count(distinct t4.id) when 0 then '---' else cast(count(distinct t4.id) as varchar) end)  as chave4
from chaves c
left outer join tabela t1 on t1.chave = c.id and t1.chave = 1
left outer join tabela t2 on t2.chave = c.id and t2.chave = 2
left outer join tabela t3 on t3.chave = c.id and t3.chave = 3
left outer join tabela t4 on t4.chave = c.id and t4.chave = 4
group by c.id

a coluna 3, exemplifica o retorno do null,

a coluna 4, o retorno convertido para varchar.

Coloquei no SQL Fiddle: http://sqlfiddle.com/#!6/c2cb0/12

1

Não sei se você vai achar gambiarra mas pode checar se o retorno é 0 e retornar vazio:

select 
F.C37 [Pontuação],
CASE WHEN ISNULL(count(distinct TD.UsuIDCliente),'-') = 0 THEN '' ELSE CONVERT(VARCHAR(15),ISNULL(count(distinct TD.UsuIDCliente),'-')) [Qtde_Clientes Detratores],
CASE WHEN ISNULL(count(distinct TN.UsuIDCliente),'-') = 0 THEN '' ELSE CONVERT(VARCHAR(15),ISNULL(count(distinct TN.UsuIDCliente),'-')) [Qtde_Clientes Neutro],
CASE WHEN ISNULL(count(distinct TP.UsuIDCliente),'-') = 0 THEN '' ELSE CONVERT(VARCHAR(15),ISNULL(count(distinct TP.UsuIDCliente),'-')) [Qtde_Clientes Promotores]
2
  • ainda retornou 0 17/08/2017 às 20:59
  • @RenanBessa, fiz uma mudança, vê se resolve
    – rLinhares
    17/08/2017 às 21:05

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .