-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1661-AverageTimeOfProcessPerMachine.sql
More file actions
40 lines (38 loc) · 1.98 KB
/
1661-AverageTimeOfProcessPerMachine.sql
File metadata and controls
40 lines (38 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
Write a solution to find the average time each machine takes to complete a process. The time to complete a process is the 'end' timestamp minus the
'start' timestamp. The average time is calculated by the total time to complete every process on the machine divided by the number of processes that
were run. The resulting table should have the machine_id along with the average time as processing_time, which should be rounded to 3 decimal places.
Return the result table in any order.The result format is in the following example.
Example 1:
Input:
Activity table:
+------------+------------+---------------+-----------+
| machine_id | process_id | activity_type | timestamp |
+------------+------------+---------------+-----------+
| 0 | 0 | start | 0.712 |
| 0 | 0 | end | 1.520 |
| 0 | 1 | start | 3.140 |
| 0 | 1 | end | 4.120 |
| 1 | 0 | start | 0.550 |
| 1 | 0 | end | 1.550 |
| 1 | 1 | start | 0.430 |
| 1 | 1 | end | 1.420 |
| 2 | 0 | start | 4.100 |
| 2 | 0 | end | 4.512 |
| 2 | 1 | start | 2.500 |
| 2 | 1 | end | 5.000 |
+------------+------------+---------------+-----------+
Output:
+------------+-----------------+
| machine_id | processing_time |
+------------+-----------------+
| 0 | 0.894 |
| 1 | 0.995 |
| 2 | 1.456 |
+------------+-----------------+
*/
SELECT *
FROM Activity a1
JOIN Activity a2
ON a1.machine_id = a2.machine_id AND a1.process_id = a2.process_id AND a1.activity_type='start' AND a2.activity_type='end'
GROUP BY a1.machine_id