You are given a Calls table with information about phone calls made in different cities.
Table: Calls
| Column Name | Type |
|---|---|
| caller_id | int |
| recipient_id | int |
| call_time | datetime |
| city | varchar |
(caller_id, recipient_id, call_time) is the primary key for this table.
Write a SQL solution to find the peak calling hour for each city. If multiple hours have the same number of calls, all of those hours will be recognized as peak hours for that specific city.
Return the result table ordered by peak calling hour and city in descending order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
caller_id
PK
|
int | ID of the person making the call |
recipient_id
PK
|
int | ID of the person receiving the call |
call_time
PK
|
datetime | Timestamp when the call was made |
city
|
varchar | City where the call originated |
Input & Output
| caller_id | recipient_id | call_time | city |
|---|---|---|---|
| 1 | 2 | 2023-01-01 09:30:00 | New York |
| 3 | 4 | 2023-01-01 09:45:00 | New York |
| 5 | 6 | 2023-01-01 10:15:00 | New York |
| 7 | 8 | 2023-01-01 14:30:00 | Los Angeles |
| 9 | 10 | 2023-01-01 14:45:00 | Los Angeles |
| 11 | 12 | 2023-01-01 15:20:00 | Los Angeles |
| city | peak_calling_hour |
|---|---|
| Los Angeles | 14 |
| New York | 9 |
For New York: Hour 9 has 2 calls, hour 10 has 1 call. Peak is hour 9 with 2 calls. For Los Angeles: Hours 14 and 15 each have 1 call, so both are peak hours. Results ordered by peak_calling_hour DESC, city DESC.
| caller_id | recipient_id | call_time | city |
|---|---|---|---|
| 1 | 2 | 2023-01-01 08:30:00 | Chicago |
| 3 | 4 | 2023-01-01 08:45:00 | Chicago |
| 5 | 6 | 2023-01-01 17:15:00 | Chicago |
| 7 | 8 | 2023-01-01 17:30:00 | Chicago |
| city | peak_calling_hour |
|---|---|
| Chicago | 17 |
| Chicago | 8 |
Chicago has 2 calls at hour 8 and 2 calls at hour 17. Both hours tie for the maximum, so both are returned as peak hours. Results ordered by hour DESC, city DESC.
Constraints
-
1 ≤ caller_id, recipient_id ≤ 1000 -
call_timeis a valid datetime -
cityis a non-empty varchar -
Each
(caller_id, recipient_id, call_time)combination is unique