Skip to content

Commit 56f2819

Browse files
committed
task: #1661
1 parent 4e77eb9 commit 56f2819

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Have a good contributing!
6565
- [1581. Customer Who Visited but Did Not Make Any Transactions](./leetcode/easy/1581.%20Customer%20Who%20Visited%20but%20Did%20Not%20Make%20Any%20Transactions.sql)
6666
- [1587. Bank Account Summary II](./leetcode/easy/1587.%20Bank%20Account%20Summary%20II.sql)
6767
- [1633. Percentage of Users Attended a Contest](./leetcode/easy/1633.%20Percentage%20of%20Users%20Attended%20a%20Contest.sql)
68+
- [1661. Average Time of Process per Machine](./leetcode/easy/1661.%20Average%20Time%20of%20Process%20per%20Machine.sql)
6869
- [1667. Fix Names in a Table](./leetcode/easy/1667.%20Fix%20Names%20in%20a%20Table.sql)
6970
- [1683. Invalid Tweets](./leetcode/easy/1683.%20Invalid%20Tweets.sql)
7071
- [1693. Daily Leads and Partners](./leetcode/easy/1693.%20Daily%20Leads%20and%20Partners.sql)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Question 1661. Average Time of Process per Machine
3+
Link: https://leetcode.com/problems/average-time-of-process-per-machine/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+
Table: Activity
6+
7+
+----------------+---------+
8+
| Column Name | Type |
9+
+----------------+---------+
10+
| machine_id | int |
11+
| process_id | int |
12+
| activity_type | enum |
13+
| timestamp | float |
14+
+----------------+---------+
15+
The table shows the user activities for a factory website.
16+
(machine_id, process_id, activity_type) is the primary key (combination of columns with unique values) of this table.
17+
machine_id is the ID of a machine.
18+
process_id is the ID of a process running on the machine with ID machine_id.
19+
activity_type is an ENUM (category) of type ('start', 'end').
20+
timestamp is a float representing the current time in seconds.
21+
'start' means the machine starts the process at the given timestamp and 'end' means the machine ends the process at the given timestamp.
22+
The 'start' timestamp will always be before the 'end' timestamp for every (machine_id, process_id) pair.
23+
It is guaranteed that each (machine_id, process_id) pair has a 'start' and 'end' timestamp.
24+
25+
26+
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.
27+
28+
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.
29+
30+
The resulting table should have the machine_id along with the average time as processing_time, which should be rounded to 3 decimal places.
31+
32+
Return the result table in any order.
33+
*/
34+
35+
WITH end_process AS (
36+
SELECT
37+
tmp.machine_id,
38+
tmp.process_id,
39+
tmp.timestamp AS end_timestamp
40+
FROM Activity AS tmp
41+
WHERE tmp.activity_type = 'end'
42+
)
43+
44+
SELECT
45+
a.machine_id,
46+
ROUND(AVG(e.end_timestamp - a.timestamp)::numeric, 3) AS processing_time
47+
FROM Activity AS a
48+
LEFT JOIN
49+
end_process AS e
50+
ON
51+
a.machine_id = e.machine_id
52+
AND a.process_id = e.process_id
53+
WHERE a.activity_type = 'start'
54+
GROUP BY a.machine_id

0 commit comments

Comments
 (0)