I have this table:
| Site_ID | Volume | RPT_Date | RPT_Hour |
|---|---|---|---|
| 1 | 10 | 01/01/2021 | 1 |
| 1 | 7 | 01/01/2021 | 2 |
| 1 | 13 | 01/01/2021 | 3 |
| 1 | 11 | 01/16/2021 | 1 |
| 1 | 3 | 01/16/2021 | 2 |
| 1 | 5 | 01/16/2021 | 3 |
| 2 | 9 | 01/01/2021 | 1 |
| 2 | 24 | 01/01/2021 | 2 |
| 2 | 16 | 01/01/2021 | 3 |
| 2 | 18 | 01/16/2021 | 1 |
| 2 | 7 | 01/16/2021 | 2 |
| 2 | 1 | 01/16/2021 | 3 |
I need to select the RPT_Hour with the highest Volume for each set of dates
Needed Output:
| Site_ID | Volume | RPT_Date | RPT_Hour |
|---|---|---|---|
| 1 | 13 | 01/01/2021 | 1 |
| 1 | 11 | 01/16/2021 | 1 |
| 2 | 24 | 01/01/2021 | 2 |
| 2 | 18 | 01/16/2021 | 1 |
SELECT site_id, volume, rpt_date, rpt_hour
FROM (SELECT t.*,
ROW_NUMBER()
OVER (PARTITION BY site_id, rpt_date ORDER BY volume DESC) AS rn
FROM MyTable) t
WHERE rn = 1;
I cannot figure out how to group the table into like date groups. If I could do that, I think the rn = 1 will return the highest volume row for each date.