Each row of call_records is one phone call placed by a user. Return the average call duration in seconds for each user on each day. A call's duration is the number of seconds between call_start and call_end, and its day is the date portion of call_start. Calls where either timestamp is missing are ignored.
Input Table(s)
Table call_records
| Column | Type | Meaning |
|---|
| call_id | INT | One id per call (primary key) |
| user_id | INT | User who placed the call, NOT NULL |
| call_start | TIMESTAMP | When the call started. Can be NULL |
| call_end | TIMESTAMP | When the call ended. Can be NULL |
Primary key: call_id.
Output Contract
One row per (user, day) that has at least one complete call, with these columns in this order:
-
user_id
-
call_day
: the date of
call_start
, as a DATE.
-
avg_duration_seconds
: the average duration in seconds of that user's complete calls on that day, rounded to 2 decimal places.
Order by user_id ascending, then call_day ascending. Each (user_id, call_day) pair appears once, so this order is unique.
Example
call_records
| call_id | user_id | call_start | call_end |
|---|
| 1 | 1 | 2024-03-01 09:00:00 | 2024-03-01 09:02:00 |
| 2 | 1 | 2024-03-01 14:30:00 | 2024-03-01 14:30:45 |
| 3 | 1 | 2024-03-01 23:59:30 | 2024-03-02 00:00:40 |
| 4 | 1 | 2024-03-02 08:00:00 | NULL |
| 5 | 2 | 2024-03-01 10:00:00 | 2024-03-01 10:05:00 |
| 6 | 2 | NULL | 2024-03-01 11:00:00 |
| 7 | 2 | 2024-03-02 16:00:00 | 2024-03-02 16:00:10 |
Expected result:
| user_id | call_day | avg_duration_seconds |
|---|
| 1 | 2024-03-01 | 78.33 |
| 2 | 2024-03-01 | 300.00 |
| 2 | 2024-03-02 | 10.00 |
User 1's calls on 2024-03-01 last 120, 45 and 70 seconds, and 235 / 3 = 78.33. Call 3 ends after midnight but counts on 2024-03-01, the date it started. User 1 has no row for 2024-03-02 because call 4 has no end time, and call 6 is dropped because it has no start time.
Constraints and Clarifications
-
The table holds between 1 and 100,000 rows.
-
call_end >= call_start
whenever both are present.
-
A call with a NULL
call_start
or a NULL
call_end
is ignored entirely. A user-day whose calls are all incomplete does not appear in the output.
-
call_id
is unique, so there are no duplicate rows. Two separate calls with identical timestamps both count.
-
All timestamps share one time zone and carry whole seconds.