All problems

Who Uses More Than One Transaction Type?

mediumSQLGroupByHaving

Product at Northline Bank is trying to work out how much of the app people actually use. The internal shorthand for an engaged customer is somebody who does more than one thing with their account — money in and money out, or a payment to somebody else — rather than a customer who only ever tops the account up and leaves it there.

The ledger will not answer that directly. It has one row per movement, and a customer with six rows might have made six identical deposits or three different kinds of movement. The number the product team cares about is the variety in a customer's rows, and nothing in the table records variety.

transactions — one row per movement of money. type says what kind of movement it was; amount is in dollars and is negative when money leaves the account.

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 a single column, customer_id, holding every customer whose movements cover at least two different type values. Somebody with six movements all of the same kind does not qualify. Hand the ids back smallest first.

Example output — shape only, on invented ids.

customer_id
6
8
9

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

Discussion

Sign in to join the discussion — reading is open to everyone.

Loading comments…