Whose Deposits Make Up Our Cash Inflow?
Treasury at Northline Bank is checking how concentrated its incoming cash is. If one customer is responsible for most of the money coming in, the bank's funding is more fragile than the headline deposit figure suggests — so what they want is not each customer's deposits but each customer's slice of the whole.
That makes every row's answer depend on a number computed from all the rows at once, which is the difficulty. A customer's own deposits are easy; the denominator is the total across everybody, and it has to be available next to each customer's figure rather than fetched separately and pasted in.
Only deposits count. Withdrawals and the one transfer are money going the other way and are not part of cash in. Note that one customer made two separate deposits.
transactions — one row per money movement. amount is signed: positive for money in, negative for money out. type is one of deposit, withdrawal or transfer.
| 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, customer_id and deposit_share, with one row per customer who has made at least one deposit. deposit_share is that customer's total deposits as a fraction of all customers' deposits — a number between 0 and 1, not a percentage, rounded to 4 decimal places. Rows for movements that are not deposits are excluded before anything is added up, so they affect neither the customer's figure nor the total. Rows come back sorted by customer_id, smallest first.
Example output — shape only, on invented customers. The shares always come to 1 between them.
| customer_id | deposit_share |
|---|---|
| 6 | 0.5 |
| 7 | 0.3125 |
| 9 | 0.1875 |
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