Each customer's largest single expense, with tie-breakers
The task
The fraud team wants to see each customer's biggest purchase. An expense is a transaction with a negative amount whose category is dining, groceries or travel. Look across all accounts of the customer.
For each customer with at least one expense, return their single largest expense (the most negative amount). If two expenses have the same amount, take the earlier posted_at. If they also share posted_at, take the lower txn_id. Each customer has exactly one row.
Return full_name, txn_id, category, merchant, spent (the amount as a positive number) and posted_at. Sort by spent descending, then full_name ascending.
Expected output
full_nametxn_idcategorymerchantspentposted_at- Sorted by spent descending, then full_name ascending.
- Checked on the sample and on 2 hidden datasets. How grading works
Tables Digital bank · click a name to insert it
customers70 rows in the sample
One row per customer. Key: customer_id.
| int | Unique id. | |
| text | Name. | |
| date | Day they became a customer. | |
| text | retail, premium or business. |
accounts126 rows in the sample
One row per account. A customer can have several. Key: account_id.
| int | Unique id. | |
| int | Owner. | |
| text | checking, savings or credit. | |
| date | Opening day. | |
| date | Closing day. NULL while open. |
transactions9,906 rows in the sample
One row per posted transaction. Positive amounts are money in, negative are money out. Key: txn_id.
| bigint | Unique id. | |
| int | The account. | |
| timestamp | When it posted (UTC). | |
| numeric(12,2) | Signed amount, USD. | |
| text | salary, rent, groceries, dining, travel, transfer, fees or refund. | |
| text | Merchant name. NULL for salary, transfers and fees. |
Related questions
- Favourite spending category per customer, counted by segmenthard
- Average first purchase value by campaign, organic includedmedium
- Genre of each listener's first playmedium
- Months each merchant ranked in the top three for spendmedium
- Repeat contact within 14 days of a first tickethard
- Best seller on each sales channel in the first quartermedium
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL