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.
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