Can anyone tell me why the result of the query below is bringing the Balance to zero in the first 4 records? I'm calculating the inputs plus the outputs. Calculates correctly if there are values in the two columns, but when there is no value in the [SAI] column the result also gets 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