Select dt1.Cuenta,dt1.Descarga,Sum(Balance) From (Select A.Cuenta,A.Descripcion, Isnull((Select Balance From CNTBal03 Where Cuenta=A.Cuenta And Periodo=12 and Fiscal='2003' ),0) +Isnull((Select Sum(Debito-Credito) From CNTDET02 Where Cuenta=A.Cuenta And Periodo=1 And Fiscal='2004'),0)+ Isnull((Select Sum(Debito-Credito) From CNTDET02 Where Cuenta IN (Select Cuenta From CNTCuentas Where Descarga=A.Cuenta) And Periodo=1 And Fiscal='2004'),0) As Balance, A.Descarga From cntcuentas A) dt1 Group by dt1.cuenta,dt1.descarga Order By rTrim(lTrim(dt1.Cuenta))But I'm still having problems at the top levels, the totals are ok in the lower level but the upper levels are still getting wrong results.
Account Parent 1 10 1 100 10 100-01 100 100-02 100 100-03 100 100-04 100 100-04-01 100-04 100-04-02 100-04 100-04-03 100-04The totals for all sub accounts are correct but the totals for account 1,10 and 100 are incorrect. Anyway I think I'm closer now.