Write a SQL query to identify the top 3 hours of peak demand for each region using window functions.
Use the regions and hourly_demand_readings tables. Aggregate readings recorded for the same region and hour, exclude regions without readings, and resolve equal demand totals by earlier hour first.
region_name, demand_hour, total_demand_mw, and peak_rankregion_name, peak_rank, and demand_hour| Column | Type | Description |
|---|---|---|
| region_idPK | INT | Unique identifier for a market region |
| region_name | VARCHAR(100) | Display name of the market region |
| Column | Type | Description |
|---|---|---|
| reading_idPK | INT | Unique identifier for a demand reading |
| region_id | INT | Region associated with the reading |
| reading_hour | TIMESTAMP | Hour represented by the demand reading |
| demand_mw | NUMERIC(10,2) | Demand measured in megawatts |