Ticket Resolution Latency: Average Days to Close and Open Tickets by Priority
Company: Salesforce
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Each row of `tickets` is one support ticket. A ticket is open while its `close_date` is `NULL`.
Write a query returning, per `priority`, the average days to close tickets, the total ticket count, and the open ticket count.
```hint Open tickets and the average
Work out what `close_date - open_date` evaluates to for an open ticket, and how `AVG` treats that value.
```
### Input Table(s)
`tickets` (primary key `ticket_id`)
| Column | Type | Meaning |
|---|---|---|
| `ticket_id` | INT | Ticket identifier, primary key |
| `priority` | VARCHAR(20) | One of `'Critical'`, `'High'`, `'Medium'`, `'Low'`; NOT NULL |
| `open_date` | DATE | Day the ticket was opened; NOT NULL |
| `close_date` | DATE | Day the ticket was closed; NULL while the ticket is open |
| `assigned_to` | VARCHAR(100) | Agent who owns the ticket; nullable, not needed for this query |
### Output Contract
- Columns, in this order: `priority`, `avg_days_to_close`, `total_tickets`, `open_tickets`.
- One row for each `priority` value that appears in `tickets`.
- `avg_days_to_close` is the mean of `close_date - open_date` in days over the closed tickets of that priority, rounded to 2 decimal places. It is `NULL` when the priority has no closed tickets.
- `total_tickets` counts every ticket of that priority. `open_tickets` counts the ones whose `close_date` is `NULL`.
- Order rows by severity: `Critical`, `High`, `Medium`, `Low`. Each priority appears once, so this order is unique.
### Example
`tickets`
| ticket_id | priority | open_date | close_date | assigned_to |
|---|---|---|---|---|
| 1 | High | 2024-03-01 | 2024-03-04 | ana |
| 2 | Critical | 2024-03-02 | 2024-03-02 | ben |
| 3 | High | 2024-03-03 | NULL | ana |
| 4 | Low | 2024-03-01 | NULL | NULL |
| 5 | High | 2024-03-05 | 2024-03-07 | carl |
| 6 | Critical | 2024-03-06 | 2024-03-09 | ben |
| 7 | Medium | 2024-03-01 | 2024-03-11 | dee |
| 8 | Medium | 2024-03-04 | 2024-03-05 | dee |
| 9 | Medium | 2024-03-08 | 2024-03-10 | ana |
Expected result:
| priority | avg_days_to_close | total_tickets | open_tickets |
|---|---|---|---|
| Critical | 1.50 | 2 | 0 |
| High | 2.50 | 3 | 1 |
| Medium | 4.33 | 3 | 0 |
| Low | NULL | 1 | 1 |
Critical's two tickets took 0 and 3 days. High averages its two closed tickets (3 and 2 days) and counts ticket 3 as open. Medium's tickets took 10, 1 and 2 days, which averages 4.333... and rounds to 4.33. Low has only an open ticket, so its average is `NULL`.
### Constraints and Clarifications
- `tickets` has between 1 and 10,000 rows.
- `close_date` is `NULL` for open tickets; otherwise it is a valid date on or after `open_date`.
- A ticket closed on the day it opened took 0 days and counts as closed.
- Open tickets count toward `total_tickets` and `open_tickets` but never toward the average.
- A priority with no tickets at all produces no row.
- `ticket_id` is the primary key, so every ticket appears exactly once.
Overview: A SQL problem on a support tickets table: for each priority, report the average days to close, the total ticket count and the number of open tickets, sorted by severity. It tests conditional aggregation, how NULL close dates affect an average, and sorting by a custom order.