2
Can anyone tell me why the result of the consultation below is bringing in the first 4 records the Zeroed Balance? I’m calculating the entrances and the exits. Calculate correctly if there are values in both columns, but when there is no value in the [SAI] column the result is also zero.
What I expected: ENT=10 + SAI=0 => SLD=10
Query:
select t1.Z1_COD,
(SELECT SUM(t2.Z1_QTD) FROM SZ1010 t2 WHERE t1.Z1_COD=t2.Z1_COD AND t2.Z1_TPMOV = 'E' ) as ENT,
(SELECT SUM(t3.Z1_QTD) FROM SZ1010 t3 WHERE t1.Z1_COD=t3.Z1_COD AND t3.Z1_TPMOV = 'S' ) as SAI,
(SELECT SUM(t2.Z1_QTD) FROM SZ1010 t2 WHERE t1.Z1_COD=t2.Z1_COD AND t2.Z1_TPMOV = 'E' ) + (SELECT SUM(t3.Z1_QTD) FROM SZ1010 t3 WHERE t1.Z1_COD=t3.Z1_COD AND t3.Z1_TPMOV = 'S' ) as SLD
FROM SZ1010 t1
And the same Z1_COD appearing several times in the first column, is it normal? I think this query will need to undergo some kind of restructuring.
– Piovezan
Test the
SUM
as follows:SUM(ISNULL(t2.Z1_QTD, 0))
– Sorack
There are likely to be values of
SAI
that isnull
on the bench, and these are being inferred as0
at some point in the result, but in the formula is nullifying the result as well.– Diego Rafael Souza