Skip to content

2984. Find Peak Calling Hours for Each City

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
WITH
  CityHourCount AS (
    SELECT
      city,
      HOUR(call_time) AS call_hour,
      COUNT(*) AS number_of_calls
    FROM Calls
    GROUP BY 1, 2
  ),
  RankedCityHourCount AS (
    SELECT
      *,
      RANK() OVER(
        PARTITION BY city
        ORDER BY number_of_calls DESC
      ) AS `rank`
    FROM CityHourCount
  )
SELECT
  city,
  call_hour AS peak_calling_hour,
  number_of_calls
FROM RankedCityHourCount
WHERE `rank` = 1
ORDER BY 2 DESC, 1 DESC;