Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
Return 0 when some information DOESN'T EXIST (not when exists and return null)

everyone. I have a query that returns a table containing values I need. That's good. But I also need, for each date already returned, that it returns also the velue 0 for qtde_produzida (that means “quantity produced”) in the days that there is nothing produced for that op_filha. For exemple: in this case below, the filters return to me two subproducts (each subproduct with one op_filha), and also return the quantity produced for each one (obviously, for the days that there was production), but I'd like to see the value 0 for those days that didn't have production. Is it possible?

My query:

DECLARE @dtInicial1 AS varchar(11), @dtFinal1 AS varchar(11), @opId1 AS INT, @setorId1 AS INT

SET @dtInicial1 = '2022-04-20'
SET @dtFinal1 = '2022-05-24'
SET @opId1 = 101855
SET @setorId1 = 6

SELECT
    DISTINCT MAX(CONVERT(varchar, C.con_dt_fim, 103)) AS data_final --final_date
    ,SUM(C.con_qtde_fim) AS qtde_produzida --qtty-produced
    ,R.rast AS op_filha --id for subproduct
    ,SP.Descricao AS descricao --subproduct's name
FROM
    Rastreio AS R
    INNER JOIN Contagem AS C ON C.rastreio_id = R.rast_id AND C.con_dt_exc IS NULL AND C.con_qtde_fim > 0
    INNER JOIN [DBG].[dbo].SubProduto AS SP ON OP.Cod_Produto = SP.CodigoSubProduto
WHERE
    R.op_id = @opId1
    AND OP.Cod_Setor = @setorId1
    AND FORMAT(C.con_dt_fim, 'MM-dd') BETWEEN FORMAT(CAST(@dtInicial1 AS DATE), 'MM-dd') AND FORMAT(CAST(@dtFinal1 AS DATE), 'MM-dd')
GROUP BY
    R.rast
    ,SP.Descricao
    ,R.rast_processo_sequencia
ORDER BY
    data_final

Printscreen of what is being returned: enter image description here

It is for a Chart.js' graph. I imagine a query that would return something like this: enter image description here

Does someone know how to do that, please? Thank you all for reading.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You just need an ISNULL. Try this:

SUM(ISNULL(C.con_qtde_fim,0)) AS qtde_produzida

or

ISNULL(SUM(C.con_qtde_fim),0) AS qtde_produzida
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda