Average Ticket Resolution Time in Minutes by Creation Date
Company: ServiceNow
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
For each creation date, return `created_date` and `avg_resolve_minutes`: the average number of minutes from `created_at` to `resolved_at` for the tickets created on that date, rounded to 2 decimal places. Exclude unresolved tickets (`resolved_at IS NULL`). Order by `created_date` ascending.
### Input Table(s)
Table `tickets` (primary key `ticket_id`):
| Column | Type | Meaning |
|---|---|---|
| ticket_id | INT | Ticket identifier |
| created_at | TIMESTAMP | When the ticket was opened, NOT NULL |
| resolved_at | TIMESTAMP | When the ticket was resolved; NULL while it is still open |
### Output Contract
- Columns, in this order: `created_date` (DATE), `avg_resolve_minutes` (numeric, 2 decimal places).
- One row per calendar date of `created_at` that has at least one resolved ticket.
- `avg_resolve_minutes` is the mean of `resolved_at - created_at` over that date's resolved tickets, expressed in minutes, with each ticket weighted equally.
- Order by `created_date` ascending. Each date appears once, so the order is unique.
### Example
`tickets`
| ticket_id | created_at | resolved_at |
|---|---|---|
| 1 | 2024-03-01 09:00:00 | 2024-03-01 09:30:00 |
| 2 | 2024-03-01 14:00:00 | 2024-03-01 15:15:30 |
| 3 | 2024-03-01 16:00:00 | NULL |
| 4 | 2024-03-02 23:50:00 | 2024-03-03 00:10:00 |
| 5 | 2024-03-03 08:00:00 | NULL |
| 6 | 2024-03-04 10:00:00 | 2024-03-04 10:00:40 |
| 7 | 2024-03-04 11:00:00 | 2024-03-04 11:20:00 |
Expected result:
| created_date | avg_resolve_minutes |
|---|---|
| 2024-03-01 | 52.75 |
| 2024-03-02 | 20.00 |
| 2024-03-04 | 10.33 |
On March 1, tickets 1 and 2 took 30 and 75.5 minutes, so the average is 52.75; ticket 3 is still open and is skipped. Ticket 4 was resolved on March 3 but counts toward March 2, the day it was created. March 3 has only an open ticket, so it has no row. On March 4 the tickets took 40 seconds and 20 minutes, which averages to 620 seconds, or 10.33 minutes.
### Constraints and Clarifications
- `tickets` has between 1 and 10,000 rows.
- `resolved_at` may be NULL (unresolved). When it is present, `resolved_at >= created_at`, so no duration is negative.
- A ticket belongs to the calendar date of its `created_at`, whatever day it is resolved on.
- Measure each duration exactly, seconds included: 90 seconds is 1.5 minutes. Do not truncate tickets to whole minutes before averaging. Round only the final average.
- A date whose tickets are all unresolved does not appear in the output.
```hint Keeping partial minutes
Subtracting two timestamps gives an interval. Convert it in a way that keeps the seconds, because the contract measures time exactly.
```
Overview: Write a PostgreSQL query that groups support tickets by the date they were created and reports the average time to resolution in minutes, skipping tickets that are still open. It tests interval arithmetic, date truncation, NULL handling and rounding an aggregate.