In the following query I need the transaction_value to be considered 0 when it is NULL in the highlighted aggregation, how can it be done? I tried using it but it did not work
SELECT num_empenho AS EMP, data_empenho AS DATA, nome_fornecedor AS FORNECEDOR, codigo_tipo AS CODIGO_TIPO, codigo_orgao AS CODIGO_ORGAO, valor_empenhado AS EMPENHADO, valor_anulado AS ANULADO, [3] AS LIQ_PERIODO, valor_liquidado AS LIQUID_ACUM, [5] AS PAGO_PERIODO,[7] AS PAGO_PERIODO2, valor_pago AS PAGO_ACUMUL, sld_a_liquidar AS A_LIQUIDAR, sld_a_pagar AS LIQ_A_PAGAR, cod_reduzido
FROM (
SELECT finmovem.num_transacao
,finmovem.codigo_tipo
,finmovem.valor_transacao
,finmovem.codigo_orgao
,finmovem.data_transacao
,finempe.cod_reduzido
,finempe.data_empenho
,finmovem.num_empenho
,finempe.nome_fornecedor
,finempe.valor_empenhado
,finempe.valor_anulado
,finempe.valor_pago
,finempe.valor_liquidado
,cast(finempe.valor_empenhado as decimal(18,2)) - cast(finempe.valor_anulado as decimal(18,2)) - cast(finempe.valor_liquidado as decimal(18,2)) as sld_a_liquidar
,cast(finempe.valor_liquidado as decimal(18,2)) - cast(finempe.valor_pago as decimal(18,2)) as sld_a_pagar
,cast(orcdotac.sld_orc_ano as decimal(18,2)) + cast(orcdotac.sld_orc_vinc as decimal(18,2)) + cast(orcdotac.sld_supl_ano as decimal(18,2)) + cast(orcdotac.sld_esp_ano as decimal(18,2)) + cast(orcdotac.sld_ext_ano as decimal(18,2)) - cast(orcdotac.sld_re_ano as decimal(18,2)) as saldo_dot_ant
FROM finmovem
INNER JOIN finempe ON
(finempe.num_empenho = finmovem.num_empenho)
INNER JOIN orcdotac ON
(finempe.cod_reduzido = orcdotac.cod_reduzido)
WHERE finmovem.codigo_tipo = 1 and finempe.codigo_tipo = 1 and finmovem.codigo_orgao = 02 and finempe.codigo_orgao = 02 and finmovem.data_transacao between '1-1-2002' and '31-12-2002' and finempe.data_empenho between '1-1-2002' and '31-12-2002'
GROUP BY finmovem.valor_transacao, finmovem.num_transacao, finmovem.codigo_orgao, finempe.cod_reduzido, finmovem.num_empenho, finempe.data_empenho, finempe.nome_fornecedor, finempe.valor_empenhado,
finempe.valor_anulado, finempe.valor_pago, finempe.valor_liquidado, orcdotac.sld_orc_ano, orcdotac.sld_orc_vinc, orcdotac.sld_supl_ano, orcdotac.sld_esp_ano, orcdotac.sld_ext_ano,
orcdotac.sld_re_ano, finmovem.codigo_tipo ,finmovem.data_transacao
) sq
PIVOT (SUM(sq.valor_transacao) FOR num_transacao IN ( [3], [5], [7])) AS pt <<< nessa agregação
ORDER BY cod_reduzido, num_empenho
In the column where the num_transaction is 3 it returns as follows:
Doing the SUM of that should return 4000 but is returning 1000.