SELECT m.[PARTITION], m.MAINACCOUNTID, SUM(a.ACCOUNTINGCURRENCYAMOUNT) AS ACCOUNTINGCURRENCYAMOUNT
FROM GENERALJOURNALACCOUNTENTRY a
JOIN GENERALJOURNALENTRY e ON e.[PARTITION] = a.[PARTITION] AND e.RECID = a.GENERALJOURNALENTRY
JOIN MAINACCOUNT m ON m.[PARTITION] = a.[PARTITION] AND m.RECID = a.MAINACCOUNT
JOIN FISCALCALENDARPERIOD p ON p.[PARTITION] = e.[PARTITION] AND p.RECID = e.FISCALCALENDARPERIOD
JOIN FISCALCALENDARYEAR FCY ON FCY.RECID = p.FISCALCALENDARYEAR
WHERE p.[TYPE] = 1 -- 0=Opening,1=Operating,2=Closing
AND m.MAINACCOUNTID = '111406'
AND FCY.NAME = '2024'
GROUP BY m.[PARTITION], m.MAINACCOUNTID
ORDER BY m.[PARTITION], m.MAINACCOUNTID