How does Sum work in Prepare scripts? I have tried a join and a sub-select without success. :(
My end goal is to use one script to get different balances on one row - Beginning Balance (everything before a particular month), Current Activity (everything in the month), and Ending Balance (Beginning Balance + Current Activity).
For example, for the period 04/2023 there would be one record for the below parameters -
My first try was to join the main query to itself using aliases and different date criteria in the WHERE clause. The main query grabs all transactions prior to 04/01/2023 for abeginning balance and the sub query pulls the data from the same table but for all transactions that equal 04/30/2023.
SELECT
[GL.Account],
[GL.AccountingEntity],
[GL.FinanceDimension1],
Sum([GL.NetTransactionAmount]),
[GL.FinanceEnterpriseGroup]
FROM
[FSM_GeneralLedgerTotal] 'GL'
WHERE [GL.Ledger]='CORE' AND [GL.PeriodEndingDate]<'04/01/2023'
AND [GL.Account]='1111000010'
AND [GL.AccountingEntity]='3100'
AND [GL.FinanceDimension1]='10000'
INNER JOIN
SELECT
[BB.Account],
[BB.AccountingEntity],
[BB.FinanceDimension1],
Sum([BB.NetTransactionAmount])
FROM
[FSM_GeneralLedgerTotal] 'BB'
WHERE
[BB.Ledger]='CORE'
AND [BB.PeriodEndingDate]='04/30/2023'
AND [BB.Account]='1111000010'
AND [BB.AccountingEntity]='3100'
AND [BB.FinanceDimension1]='10000'
ON [GL.Account]=[BB.Account]
AND [GL.AccountingEntity]=[BB.AccountingEntity]
AND [GL.FinanceDimension1]=[BB.FinanceDimension1]
But the resulting sums (GL.NetTransactionAmount and BB.NetTransactionAmount) are giving me all kinds of weird figures.
When I remove the INNER JOIN Sum([GL.NetTransactionAmount]) is correct so it is definitely a calculation happening with the JOIN.
My second try was to change tactics and try a subselect directly in the Script (like you can do in SQL) but that did not even validate -
SELECT
[GL.Account],
[GL.AccountingEntity],
[GL.FinanceDimension1],
SELECT Sum([FSM_GeneralLedger.NetTransactionAmount]) FROM [FSM_GeneralLedger] WHERE [FSM_GeneralLedger.PeriodEndingDate]<=04/01/2024',
SELECT Sum([FSM_GeneralLedger.NetTransactionAmount]) FROM [FSM_GeneralLedger] WHERE [FSM_GeneralLedger.PeriodEndingDate]='04/30/2023',
[GL.FinanceEnterpriseGroup]
FROM
[FSM_GeneralLedgerTotal] 'GL'
WHERE [GL.Ledger]='CORE'
AND [GL.Account]='1111000010'
AND [GL.AccountingEntity]='3100'
AND [GL.FinanceDimension1]='10000'
Please and thank you,
Lisa