Write a query to find the top three departments by spending in each fiscal quarter.
Use the fiscal quarter definitions provided in fiscal_quarters and include only posted expenditures. Return quarters that have at least one posted expenditure.
fiscal_year, fiscal_quarter, department_name, total_spending, and spending_rank.| Column | Type | Description |
|---|---|---|
| department_idPK | INT | Unique department identifier |
| department_code | VARCHAR(10) | Short department code |
| department_name | VARCHAR(100) | City department name |
| Column | Type | Description |
|---|---|---|
| fiscal_yearPK | INT | Fiscal year containing the quarter |
| fiscal_quarterPK | INT | Fiscal quarter number from 1 through 4 |
| start_date | DATE | Inclusive start date of the fiscal quarter |
| end_date | DATE | Inclusive end date of the fiscal quarter |
| Column | Type | Description |
|---|---|---|
| expenditure_idPK | INT | Unique expenditure identifier |
| department_id | INT | Department responsible for the expenditure |
| expenditure_date | DATE | Date the expenditure was recorded |
| amount | NUMERIC(12,2) | Expenditure amount in US dollars |
| status | VARCHAR(20) | Expenditure processing status |