Loading the editor…
Find the vendor with the highest non-void bill total in each vendor category.
Procurement wants one preferred vendor per category. Voided bills are not spend. A vendor with no category still forms a group.
Return category, vendor_name, and spend_cents. vendor_name is trimmed. spend_cents sums bills whose status is not void. Keep the top vendor per category using RANK. Sort by category.
| id | name | account_type |
|---|---|---|
| 1 | Cash | asset |
| 2 | Accounts Payable | liability |
| 3 | Sales Revenue | revenue |
| id | name | category |
|---|---|---|
| 1 | Harbor Office | Facilities |
| 2 | Northwind Cloud | Software |
| 3 | City Clinic Supplies | Medical |
| id | vendor_id | amount_cents | issued_on | due_on | paid_on | status |
|---|---|---|---|---|---|---|
| 1 | 1 | 180000 | 2025-01-02 | 2025-01-31 | 2025-01-20 | paid |
| 2 | 2 | 42000 | 2025-01-05 | 2025-02-04 | 2025-02-10 | paid |
| 3 | 2 | 42000 | 2025-02-05 | 2025-03-07 | NULL | open |
| id | account_id | txn_date | amount_cents | direction | memo | status |
|---|---|---|---|---|---|---|
| 1 | 4 | 2025-01-20 | 180000 | debit | January rent | posted |
| 2 | 5 | 2025-02-10 | 42000 | debit | Cloud january | Posted |
| 3 | 6 | 2025-01-28 | 250000 | debit | Payroll | POSTED |
Ctrl or Cmd + Enter runs the query. The database resets on every run.
Run the query to see rows from the sample database.
Next exercise: Patients admitted on their first visit
Related: Open payables by vendor, Top customer in each country