You have a table called Activity that tracks factory machine activities with the following structure:
machine_id: ID of the machineprocess_id: ID of the process running on the machineactivity_type: Either'start'or'end'timestamp: Time in seconds when the activity occurred
Goal: Calculate the average time each machine takes to complete a process. The process time is calculated as end timestamp - start timestamp, and the average is the total time divided by the number of processes.
Return the machine_id and processing_time (rounded to 3 decimal places) for each machine.
Table Schema
| Column Name | Type | Description |
|---|---|---|
machine_id
PK
|
int | ID of the machine |
process_id
PK
|
int | ID of the process running on the machine |
activity_type
PK
|
enum | Either 'start' or 'end' |
timestamp
|
float | Time in seconds when activity occurred |
Input & Output
| machine_id | process_id | activity_type | timestamp |
|---|---|---|---|
| 0 | 0 | start | 0.712 |
| 0 | 0 | end | 1.52 |
| 0 | 1 | start | 3.14 |
| 0 | 1 | end | 4.512 |
| 1 | 0 | start | 0.55 |
| 1 | 0 | end | 1.55 |
| 1 | 1 | start | 0.43 |
| 1 | 1 | end | 1.42 |
| machine_id | processing_time |
|---|---|
| 0 | 1.494 |
| 1 | 0.995 |
Machine 0 has two processes: Process 0 takes (1.520 - 0.712) = 0.808 seconds, Process 1 takes (4.512 - 3.140) = 1.372 seconds. Average: (0.808 + 1.372) / 2 = 1.090 seconds. Machine 1 has Process 0 taking (1.550 - 0.550) = 1.000 seconds and Process 1 taking (1.420 - 0.430) = 0.990 seconds. Average: (1.000 + 0.990) / 2 = 0.995 seconds.
| machine_id | process_id | activity_type | timestamp |
|---|---|---|---|
| 0 | 0 | start | 1 |
| 0 | 0 | end | 3 |
| 1 | 0 | start | 2 |
| 1 | 0 | end | 5 |
| machine_id | processing_time |
|---|---|
| 0 | 2 |
| 1 | 3 |
Each machine has only one process. Machine 0's process takes (3.0 - 1.0) = 2.000 seconds. Machine 1's process takes (5.0 - 2.0) = 3.000 seconds. Since there's only one process per machine, the average equals the single process time.
Constraints
-
1 ≤ machine_id ≤ 100 -
1 ≤ process_id ≤ 100 -
activity_typeis either'start'or'end' -
1 ≤ timestamp ≤ 1000 -
Each
(machine_id, process_id)pair has exactly one'start'and one'end'record