All problems

The Volume Leaderboard

hardSQLWindow FunctionsJoins

The private-banking desk at Northline Bank keeps a leaderboard of customers by how much money they push through the bank in either direction, and puts a position number beside each name. The desk's rule is that two customers moving identical amounts must show the same position — and the position after a shared one is skipped, so a board can read 1, 2, 2, 4.

transactions — one row per movement of money. customer_id points at a row in customers; type is one of deposit, withdrawal or transfer; txn_date is the day it settled. The ledger is signed: money arriving in an account is stored as a positive amount, and money leaving it as a negative one, so a 600-dollar withdrawal is recorded as -600.

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

customers — one row per account holder at Northline. account_type is either checking or savings; city is the branch city the account belongs to.

id name account_type city
1 Farah Idris checking Chicago
2 Grant Boyle savings Miami
3 Hana Suzuki checking Chicago
4 Ibrahim Njoku savings Miami
5 Jade Wu checking Seattle

Both tables already exist in the database — there is nothing to create or load.

Task: Write a query that returns three columns — name, volume and volume_rank — one row per customer with at least one movement. volume is the total money they moved ignoring direction, rounded to 2 decimal places; volume_rank is 1 for the largest volume, with customers on identical volumes sharing a position and the next position skipped. Return the rows lowest volume_rank first, with customers sharing a position separated alphabetically by name.

Example output — shape only, on invented customers. Two of them moved the same money, so they share position 3 and position 4 goes unused:

name volume volume_rank
Marisol Vega 15800 1
Oscar Delacroix 11200 2
Nadia Ferreira 9400 3
Tobias Renner 9400 3
Rune Halvorsen 6100 5

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…