Given a table Tasks with task information including task ID, employee ID, and start/end times, write a SQL query to find:
- Total duration of tasks for each employee (rounded down to the nearest full hour)
- Maximum number of concurrent tasks an employee handled at any point in time
The result should be ordered by employee_id in ascending order.
Note: When calculating total duration, overlapping time periods should only be counted once (no double counting).
Table Schema
| Column Name | Type | Description |
|---|---|---|
task_id
PK
|
int | Unique identifier for each task |
employee_id
PK
|
int | Identifier for the employee assigned to the task |
start_time
|
datetime | Start time of the task |
end_time
|
datetime | End time of the task |
Input & Output
| task_id | employee_id | start_time | end_time |
|---|---|---|---|
| 1 | 1001 | 2023-05-01 08:00:00 | 2023-05-01 09:00:00 |
| 2 | 1001 | 2023-05-01 08:30:00 | 2023-05-01 10:30:00 |
| 3 | 1001 | 2023-05-01 11:00:00 | 2023-05-01 12:00:00 |
| 7 | 1001 | 2023-05-01 13:00:00 | 2023-05-01 15:30:00 |
| 4 | 1002 | 2023-05-01 09:00:00 | 2023-05-01 10:00:00 |
| 5 | 1002 | 2023-05-01 09:30:00 | 2023-05-01 11:30:00 |
| 6 | 1003 | 2023-05-01 14:00:00 | 2023-05-01 16:00:00 |
| employee_id | total_task_hours | max_concurrent_tasks |
|---|---|---|
| 1001 | 6 | 2 |
| 1002 | 2 | 2 |
| 1003 | 2 | 1 |
Employee 1001: Tasks 1 and 2 overlap from 08:30-09:00 (30 minutes). Total time: 60 + 120 + 60 + 150 - 30 = 360 minutes = 6 hours. Max concurrent: 2 tasks.
Employee 1002: Tasks 4 and 5 overlap from 09:30-10:00 (30 minutes). Total time: 60 + 120 - 30 = 150 minutes = 2.5 hours → 2 hours (rounded down). Max concurrent: 2 tasks.
Employee 1003: Single task with no overlaps. Total time: 120 minutes = 2 hours. Max concurrent: 1 task.
| task_id | employee_id | start_time | end_time |
|---|---|---|---|
| 1 | 1001 | 2023-05-01 08:00:00 | 2023-05-01 09:00:00 |
| 2 | 1001 | 2023-05-01 10:00:00 | 2023-05-01 11:00:00 |
| 3 | 1002 | 2023-05-01 09:00:00 | 2023-05-01 10:30:00 |
| employee_id | total_task_hours | max_concurrent_tasks |
|---|---|---|
| 1001 | 2 | 1 |
| 1002 | 1 | 1 |
All tasks are non-overlapping. Employee 1001 has 2 tasks of 1 hour each = 2 total hours. Employee 1002 has 1 task of 1.5 hours → 1 hour (rounded down). Maximum concurrent tasks is 1 for both employees.
Constraints
-
1 ≤ task_id ≤ 10^5 -
1 ≤ employee_id ≤ 10^4 -
start_time < end_time -
start_timeandend_timeare valid datetime values