-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1661-AverageTimeOfProcessPerMachine.sql
More file actions
115 lines (107 loc) · 5.45 KB
/
1661-AverageTimeOfProcessPerMachine.sql
File metadata and controls
115 lines (107 loc) · 5.45 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- 1661. Average Time of Process per Machine
-- Table: Activity
-- +----------------+---------+
-- | Column Name | Type |
-- +----------------+---------+
-- | machine_id | int |
-- | process_id | int |
-- | activity_type | enum |
-- | timestamp | float |
-- +----------------+---------+
-- The table shows the user activities for a factory website.
-- (machine_id, process_id, activity_type) is the primary key (combination of columns with unique values) of this table.
-- machine_id is the ID of a machine.
-- process_id is the ID of a process running on the machine with ID machine_id.
-- activity_type is an ENUM (category) of type ('start', 'end').
-- timestamp is a float representing the current time in seconds.
-- 'start' means the machine starts the process at the given timestamp and 'end' means the machine ends the process at the given timestamp.
-- The 'start' timestamp will always be before the 'end' timestamp for every (machine_id, process_id) pair.
-- There is a factory website that has several machines each running the same number of processes. 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 |
-- +------------+-----------------+
-- Explanation:
-- There are 3 machines running 2 processes each.
-- Machine 0's average time is ((1.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894
-- Machine 1's average time is ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995
-- Machine 2's average time is ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456
-- Create table If Not Exists Activity (machine_id int, process_id int, activity_type ENUM('start', 'end'), timestamp float)
-- Truncate table Activity
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'start', '0.712')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '0', 'end', '1.52')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'start', '3.14')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('0', '1', 'end', '4.12')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'start', '0.55')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '0', 'end', '1.55')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'start', '0.43')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('1', '1', 'end', '1.42')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'start', '4.1')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '0', 'end', '4.512')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'start', '2.5')
-- insert into Activity (machine_id, process_id, activity_type, timestamp) values ('2', '1', 'end', '5')
-- Write your MySQL query statement below
SELECT
a.machine_id,
ROUND(
(
SELECT
avg(a1.timestamp)
FROM
Activity a1
WHERE
a1.activity_type = 'end' AND a1.machine_id = a.machine_id
)
-
(
SELECT
avg(a1.timestamp)
FROM
Activity a1
WHERE
a1.activity_type = 'start' AND a1.machine_id = a.machine_id)
,3) AS processing_time
FROM
Activity a
GROUP BY
a.machine_id
-- best solution
SELECT
a1.machine_id,
round(avg(a2.TIMESTAMP - a1.TIMESTAMP ), 3) AS processing_time
FROM
activity a1, activity a2
WHERE
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;