Hi my name is Ryan, I need to fix an accounting project summary report. I've tried everything, but the initial balance is still being calculated incorrectly.
When adding up the initial balance, the total debit and total credit from the query's result, they should match the totals in the trial balance.

This is the query I am using.
SELECT T99.PrjCode 'Project', ISNULL(SUM(X0.SaldoInicial), 0) as 'Saldo_Inicial', SUM(T0.Debit) as 'Debito', SUM(T0.Credit) as 'Credito', ISNULL(SUM(X0.SaldoInicial), 0) + SUM(T0.Debit) - SUM(T0.Credit) as 'Saldo Final' FROM OPRJ T99 LEFT JOIN JDT1 T0 ON T99.PrjCode = T0.Project LEFT JOIN ( SELECT X1.[Project] AS 'Project', -SUM(X1.Credit) + SUM(X1.Debit) AS 'SaldoInicial' FROM JDT1 X1 WHERE X1.ACCOUNT IN ('1120301', '1120401', '2160101', '2170101', '2170102') GROUP BY X1.[Project] ) X0 ON X0.Project = T99.PrjCode WHERE T0.[Account] IN ('1120301','1120401','2160101','2170101','2170102') AND T0.Project LIKE '%[%0]%' AND T0.Account LIKE '%[%1]%' AND T0.RefDate >= [%2] AND T0.RefDate <= [%3] GROUP BY T99.[PrjCode];
When I sum up the Result in an Excel spreadsheet, this is the result. Notice that the debit and credit columns match, however, the initial and final balances do not.

I believe the issue lies in the calculation of the initial balance. If you could assist me in resolving this, I would be very grateful. I've considered using the calculation formula from SAP's own trial balance. If you could teach me where I can find the query that makes SAP's trial balance calculation, I would greatly appreciate it.