How Big Is a Typical Transaction?
Fraud detection at Northline Bank works by flagging movements that are unusual for their kind, so before any threshold can be set somebody has to establish what usual looks like. Risk has asked for the typical size of each kind of movement on the ledger.
There is a catch in how the ledger is written. Amounts carry their direction in the sign — money leaving is stored as a negative number — so a withdrawal of 600 is recorded as -600. Averaged as they stand, withdrawals come out negative and cannot be compared with deposits at all, and a large withdrawal would drag the figure the wrong way. What Risk wants is the size of a movement, which is the amount with its direction set aside.
transactions — one row per movement of money. amount is in dollars and is negative when money leaves the account. type says what kind of movement it was.
| id | customer_id | amount | type | txn_date |
|---|---|---|---|---|
| 1 | 1 | 500 | deposit | 2023-07-01 |
| 2 | 1 | -120 | withdrawal | 2023-07-03 |
| 3 | 2 | 1000 | deposit | 2023-07-02 |
| 4 | 2 | -300 | withdrawal | 2023-07-05 |
| 5 | 3 | 250 | deposit | 2023-07-04 |
| 6 | 3 | -600 | withdrawal | 2023-07-06 |
| 7 | 4 | 800 | deposit | 2023-07-07 |
| 8 | 4 | -450 | withdrawal | 2023-07-08 |
| 9 | 5 | 2000 | deposit | 2023-07-09 |
| 10 | 5 | -1500 | withdrawal | 2023-07-10 |
| 11 | 1 | -200 | transfer | 2023-07-11 |
| 12 | 3 | 400 | deposit | 2023-07-12 |
The table already exists in the database — there is nothing to create or load.
Task: Write a query returning two columns, type and avg_amount, with one row for every type that appears in the ledger, rounded to 2 decimal places. avg_amount is the average size of a movement of that kind, size meaning the amount with its sign set aside, so that -600 counts as 600. A kind that never occurs produces no row. Rows come back in alphabetical order of type.
Example output — shape only; the figures below are invented, not this data's answer.
| type | avg_amount |
|---|---|
| deposit | 1240.5 |
| transfer | 375 |
| withdrawal | 612.25 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in