Below SAP Query return Trial Balance with selected date range. 



DECLARE @StartDate date = '20240101'
DECLARE @EndDate date = '20240731'

-- Group COA

IF OBJECT_ID('tempdb..#COA') IS NOT NULL DROP TABLE #COA

SELECT
T1.AcctCode,
T1.AcctName,
T1.FatherNum,
ParentAcct.AcctName AS ParentAcctName,
T1.ExportCode
INTO
#COA
FROM
OACT T1 LEFT JOIN
(SELECT
T0.AcctCode,
T0.AcctName
FROM
OACT T0
WHERE
T0.FrozenFor ='N'
AND T0.Postable='N'
) AS
ParentAcct ON ParentAcct.AcctCode = T1.FatherNum
WHERE
T1.FrozenFor ='N'
AND T1.Postable='Y'
ORDER BY
T1.AcctCode

-- Extract Journal Details
SELECT
T3.Account,
#COA.AcctName,
#COA.ParentAcctName AS Classification,
#COA.ExportCode,
SUM(T3.Debit) AS Debit,
SUM(T3.Credit) AS Debit ,
SUM(T3.Debit-T3.Credit) AS Balance
FROM
OJDT T2
INNER JOIN JDT1 T3 ON T3.TransId = T2.TransId
INNER JOIN #COA ON #COA.AcctCode = T3.Account
WHERE
T2.RefDate BETWEEN @StartDate AND @EndDate
GROUP BY
T3.Account,
#COA.AcctName,
#COA.ParentAcctName,
#COA.ExportCode
ORDER BY
T3.Account