Lesson 10 of 13 · 26 min

Transaction analysis: clean spending figures

By the end of this lesson you can build a spending measure from raw card and online transactions that excludes reversals and non-spending movements, analyse it by month and category group, and flag suspected duplicate postings.

Rawabi's transactions table contains every posting: salaries, transfers, ATM withdrawals, fees, profit payments, card purchases, and the reversals of some of those purchases. Spending in this lesson means card and online purchases:

  • channel IN ('pos', 'online') (point-of-sale and online purchases);
  • direction = 'debit' (money out of the customer's account);
  • merchant_name IS NOT NULL (online rent payments and transfers to other people are debits on the online channel too, but they have no merchant and are not purchases);
  • not reversed: a reversal is a separate credit row, 1–4 days later, with reversal_of_transaction_id pointing at the original purchase.

Internal transfers between Rawabi accounts (card repayments, savings top-ups) use the transfer channel, so the channel rule already keeps them out. Counting them would double-count money that simply moved between two accounts of the same customer.

Included with a Formula Quest membership

This lesson, its 4 graded exercises and every other Members lesson in SQL Quest come with a Formula Quest membership that includes SQL (Single Quest with SQL selected, Quest Trio including SQL, or Quest Unlimited), or with SQL access from your organization.

Free to everyone: all of SQL Foundations and Retrieving and Filtering Data, the first lesson of every track, the playground, the dataset library and the SQL reference. See free lessons