Months each merchant ranked in the top three for spend
The task
The cards team tracks which merchants dominate everyday spending. A transaction counts as everyday spending when its category is dining, groceries or travel. A merchant's spend in a calendar month is the sum of -amount over its everyday transactions posted in that month (month of posted_at). Rent, refunds, fees, salary and transfers are ignored.
For each month, rank merchants by spend, highest first. Merchants with the same spend share a rank, and the next rank skips ahead (1, 1, 3). A merchant is in the top three of a month when its rank is 3 or better, so ties at the cut-off are all included.
Return merchant and months_in_top_three, the number of months in which the merchant was in the top three. Merchants that were never in the top three do not appear. Sort by months_in_top_three descending, then merchant ascending.
Expected output
merchantmonths_in_top_three- Sorted by months_in_top_three descending, then merchant ascending.
- Checked on the sample and on 3 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
- Top two artists each month by listening timemedium
- Best seller on each sales channel in the first quartermedium
- Each customer's largest single expense, with tie-breakersmedium
- First order value by acquisition channelmedium
- How far each artist climbed or fell from April to Mayhard
Loading the editor and PostgreSQL
Loading the editor and PostgreSQL