0
By adding up any positive number with the result of a Subquery
get as return null
and need to return 0 so it is possible to add with the first value obtained.
What can I do to make the query return me an entire amount?
Segue Query:
DECLARE @TOTAL VARCHAR(100)
SET @TOTAL = (SELECT TOP 1 PTB.PATSLDCTAVALCONTAB
FROM PAT_SALDO_CONTA PTB WITH (NOLOCK), CONTAB_SALDO_MOV_CTA CS WITH (NOLOCK)
WHERE CS.PlanoCtaCodRed = PTB.PlanoCtaCodRed
AND PTB.PlanoCtaCodRed = '18872'
AND PTB.PatSldCtaAnoMes = '201809'
AND PTB.EmpCod like '%' + (CASE WHEN '01.13' = '01' THEN '01.' ELSE '01.13' END)+ '%')
+(SELECT @TOTAL
+ COALESCE((SELECT (SUM(ISNULL(CSA.ContabSaldoMovCtaValDeb,0)))
-(SUM(ISNULL(CSA.ContabSaldoMovCtaValCred,0)))
FROM CONTAB_SALDO_MOV_CTA CSA
WHERE CSA.PlanoCtaCodRed = '18872'
AND CSA.ContabSaldoMovCtaAnoMes >= '2018' + '' + '10'
AND CSA.ContabSaldoMovCtaAnoMes <= '2018' + '' + '10'
AND CSA.EmpCod like '%' + (CASE WHEN '01.13' = '01' THEN '01.' ELSE '01.13' END) + '%')
,0))
SELECT @TOTAL
I saw you use the
ISNULL()
within select... Could not use it again? for exampleTOTAL + ISNULL((SELECT....), 0)
– Matheus Ribeiro
yes but it returns 0, I wanted it to return 10, type 10 + 0 = 10
– Felipe Michael da Fonseca
Felipe, I suggest you first separate the sum to check the returned values on
total
and in thesubselect
. After finding that the problem is unique to the return of thesubselect
try to put do as Matheus suggested. It is possible that the total is different from 10 as quoted in the comment above and so the return is 0?– Caique Romero