I wasn't smart enough to fully understand how EXEC_SP worked initially so I had to go on my own personal development journey.
I have a number of BO in my model:
Contact
Contact_Bank
Contact_Bank_Statement
Finance_Cashbook
A Contact may have one or more Contact_Bank (accounts) and a Contact_Bank may have one or more Contact_Bank_Statement. A Contact_Bank_Statement has one or more statement lines which appear as records in the Finance_Cashbook. And then there is some linkage to reference data.
The (abridged) ERD looks something like this (this is obviously not an ERD but the image from the query builder I used): To calculate the current and historic balance requires a SUM across each and every transaction and that to be updated every time that a new Finance_Cashbook line is entered. Too expensive and complex to do in AIM, but not that expensive as a native SQL query.
It looks something like this in a stored procedure (the stored proc is not optimal in its method of calculating the running balance but it is fit for purpose for figuring out the tricky bits).
DEPRECATED FOR mySQL 8.0 SEE ALTERNATIVE BELOW
Code: Select all
SET @runtot:=0;
SELECT `MonthTot`.`Acc` AS `Account`,
`MonthTot`.`Curr` AS `Curr`,
`MonthTot`.`TXNYEAR` AS `Year`,
`MonthTot`.`TXNMTH` AS `Month`,
CONCAT(`MonthTot`.`TXNYEAR`, '/', `MonthTot`.`TXNMTH`) AS `Period`,
`MonthTot`.`DEBITS` AS `Money Out`,
`MonthTot`.`CREDITS` AS `Money In`,
@runtot:=@runtot+(`MonthTot`.`CREDITS`-`MonthTot`.`DEBITS`) AS `Balance`
FROM
(SELECT
`CONTACT_BANK`.`AccountNickname` AS `Acc`,
`REF_GENERAL_CURRENCY`.`ISO3` AS `Curr`,
YEAR(`FCB`.`DateTransaction`) AS `TXNYEAR`,
MONTH(`FCB`.`DateTransaction`) AS `TXNMTH`,
SUM(`FCB`.`AmtDebit`) AS `DEBITS`,
SUM(`FCB`.`AmtCredit`) AS `CREDITS`
FROM `REF_GENERAL_CURRENCY`
INNER JOIN `CONTACT_BANK` ON `REF_GENERAL_CURRENCY`.`ID` = `CONTACT_BANK`.`ps_Currency_RID`
INNER JOIN `CONTACT_BANK_STATEMENT` ON `CONTACT_BANK`.`ID` = `CONTACT_BANK_STATEMENT`.`ob_Bank_RID`
INNER JOIN `FINANCE_CASHBOOK` `FCB` ON `FCB`.`ob_Statement_RID` = `CONTACT_BANK_STATEMENT`.`ID`
WHERE `CONTACT_BANK`.`ID`=22712
GROUP BY `TXNYEAR` ASC, `TXNMTH` ASC, `ACC`, `CURR`
) AS `MonthTot`;
Code: Select all
Account, Curr, Year, Month, Period, Money Out, Money In, Balance
"EURO Account", EUR, 2018, 1, 2018/1, 20, 500, 480
"EURO Account", EUR, 2018, 3, 2018/3, 120, 200, 560
"EURO Account", EUR, 2018, 5, 2018/5 50, 0, 510
"EURO Account", EUR, 2018, 6, 2018/6, 0, 100, 610
"EURO Account", EUR, 2018, 7, 2018/7, 0, 400, 1010
"EURO Account", EUR, 2018, 8, 2018/8, 250, 0, 760
I created an object (which I named the same as the SP in the database): In order to get AIM to play nicely with the returned output the SP needs to be modified slightly to add in the Happy AIM columns: BASVERSION, BASTIMESTAMP and ID:
Code: Select all
SET @runtot:=0;
SET @ID:=0;
SELECT 1 AS `BASVERSION`,
CURRENT_TIMESTAMP AS `BASTIMESTAMP`,
@ID:=@ID+1 AS `ID`,
`MonthTot`.`Acc` AS `AccountNick`,
`MonthTot`.`Curr` AS `CurrISO`,
`MonthTot`.`TXNYEAR` AS `Year`,
`MonthTot`.`TXNMTH` AS `Month`,
CONCAT(`MonthTot`.`TXNYEAR`, '/', `MonthTot`.`TXNMTH`) AS `Period`,
`MonthTot`.`DEBITS`*-1 AS `MoneyOut`,
`MonthTot`.`CREDITS` AS `MoneyIn`,
@runtot:=@runtot+(`MonthTot`.`CREDITS`-`MonthTot`.`DEBITS`) AS `Balance`
Last step was to set up the query in AIM:
Code: Select all
EXEC_SP 'CB_GetTxnBalByMth' WITH '@AccountID'=LoggedInRegularUser.VPL_FinBankAcc.ID RETURN CB_GetTxnBalByMth
I wanted to put all the various contributions, suggestions and experience posts by everyone else explaining all the moving parts into one worked example and hopefully save anyone coming behind me some time.
My reading list:
Bruce's Post on his journey through Stored Procedures https://www.awareim.com/forum/viewtopic ... 40&p=43493
UnionSystems work around for converting arrays into strings for passing to input parameters in stored Procedures (and a link to Bruce's video on SP use, which I've only just seen) https://www.awareim.com/forum/viewtopic ... 22&p=43414
Capturing the BAS fields https://www.awareim.com/forum/viewtopic ... 63&p=43078
Some Speed questions re FIND v SP https://www.awareim.com/forum/viewtopic ... 69&p=39290