SELECT trim(to_char(date_trunc('month', ao.invoice_date), 'Month')) || to_char(date_trunc('month', ao.invoice_date), ' YYYY') AS "Month"
    ,to_char(sum(CASE 
                WHEN ao.transaction_category = 'DEBIT'
                    THEN ao.amount
                ELSE 0
                END), '99999999990.00') AS "Amount DEBIT"
    ,to_char(sum(CASE 
                WHEN ao.transaction_category = 'CREDIT'
                    THEN ao.amount
                ELSE 0
                END), '99999999990.00') AS "Amount CREDIT"
    ,to_char(sum(CASE 
                WHEN ao.transaction_category = 'DEBIT'
                    THEN ao.amount
                ELSE - ao.amount
                END), '99999999990.00') AS "Balance"
FROM ar_account_operation ao
WHERE ao.invoice_date IS NOT NULL
AND ao.invoice_date > (date_trunc('month', CURRENT_DATE) - CAST(:MONTH_INTERVAL || ' month' AS INTERVAL))
GROUP BY date_trunc('month', ao.invoice_date)
ORDER BY date_trunc('month', ao.invoice_date) DESC