RienSQL
TASK #220 · Hard · BBVA

Balance by Product Type

Your brief
The current balance of an account is start_balance plus received transfers (transfers.to_account_id), minus sent transfers (transfers.from_account_id) and card payments (card_payments). For an account without operations it equals start_balance.

Write a query that, for each product type, returns product, the number of accounts accounts_count, the sum of current balances total_balance and the average current balance per account avg_balance. Round total_balance and avg_balance to two decimals.
Expected columnsproduct accounts_count total_balance avg_balanceBBVA
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Schema
customerscustomer_id, full_name, city, joined_date
accountsaccount_id, customer_id, product, opened_date, start_balance
card_paymentspayment_id, account_id, paid_at, merchant, category, amount
transferstransfer_id, from_account_id, to_account_id, transfer_at, amount, concept