Write a query to find the top three most expensive vendors per department, handling ties appropriately.
Calculate each vendor's total invoice amount within each department. Include vendors with tied totals when they fall within the top three spending levels.
department_name, vendor_name, total_spend, and spend_rank| Column | Type | Description |
|---|---|---|
| department_idPK | INT | Unique department identifier |
| department_name | VARCHAR(100) | Department name |
| Column | Type | Description |
|---|---|---|
| vendor_idPK | INT | Unique vendor identifier |
| vendor_name | VARCHAR(150) | Vendor name |
| Column | Type | Description |
|---|---|---|
| invoice_idPK | INT | Unique invoice identifier |
| vendor_id | INT | Referenced vendor identifier |
| department_id | INT | Referenced department identifier |
| amount | DECIMAL(12,2) | Invoice amount |