SELECT Banks.BankName, ; SUM(IIF(INLIST(TranCode, cCredit1, cCredit2, cCredit3), ; Collections.Amount, $0)) AS Credit_amt, ; ... ; FROM Banks LEFT JOIN Collections ; ON Banks.BankId = Collections.BankID ; AND BETWEEN(Collections.SetDate, m.dStartDate, m.dEndDate)Note the use of $0 in the SUM to maintain the Currency data type. SQL uses the type and size of the first record that meets the condition. Be prepared for your first record to be a 0.
>SELECT collections.settdate, collections.bankname, collections.portfolio, ; MONTH(collections.settdate) AS mymonth, ; SUM(IIF(BETWEEN(collections.settdate, m.dStartDate, m.dEndDate) AND (trancode = cCredit1 OR trancode =cCredit2 OR trancode =cCredit3), 1 , 0)) AS credits , ; SUM(IIF(BETWEEN(collections.settdate, m.dStartDate, m.dEndDate) AND (trancode = cCredit1 OR trancode =cCredit2 OR trancode =cCredit3), collections.amount ,0.00)) AS credit_amt, ; SUM(IIF(BETWEEN(collections.settdate, m.dStartDate, m.dEndDate) AND (trancode = cDebit1 OR trancode =cDebit2 OR trancode =cDebit3), 1 , 0)) AS debits , ; SUM(IIF(BETWEEN(collections.settdate, m.dStartDate, m.dEndDate) AND (trancode = cDebit1 OR trancode =cDebit2 OR trancode =cDebit3), collections.amount ,0.00)) AS debit_amt, ; SUM(IIF(BETWEEN(collections.settdate, m.dStartDate, m.dEndDate) ,1,0)) AS tot_num, ; SUM(IIF(BETWEEN(collections.settdate, m.dStartDate, m.dEndDate) ,collections.amount ,0.00)) AS tot_amount ; FROM collections ; WHERE BETWEEN(collections.settdate, m.dStartDate, m.dEndDate) and collections.portfolio= cBank ; GROUP BY collections.settdate, collections.bankname ; INTO TABLE eraseme2>