Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Have a good contributing!
- [1581. Customer Who Visited but Did Not Make Any Transactions](./leetcode/easy/1581.%20Customer%20Who%20Visited%20but%20Did%20Not%20Make%20Any%20Transactions.sql)
- [1587. Bank Account Summary II](./leetcode/easy/1587.%20Bank%20Account%20Summary%20II.sql)
- [1633. Percentage of Users Attended a Contest](./leetcode/easy/1633.%20Percentage%20of%20Users%20Attended%20a%20Contest.sql)
- [1661. Average Time of Process per Machine](./leetcode/easy/1661.%20Average%20Time%20of%20Process%20per%20Machine.sql)
- [1667. Fix Names in a Table](./leetcode/easy/1667.%20Fix%20Names%20in%20a%20Table.sql)
- [1683. Invalid Tweets](./leetcode/easy/1683.%20Invalid%20Tweets.sql)
- [1693. Daily Leads and Partners](./leetcode/easy/1693.%20Daily%20Leads%20and%20Partners.sql)
Expand Down
54 changes: 54 additions & 0 deletions leetcode/easy/1661. Average Time of Process per Machine.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Question 1661. Average Time of Process per Machine
Link: https://leetcode.com/problems/average-time-of-process-per-machine/description/?envType=study-plan-v2&envId=top-sql-50

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.
It is guaranteed that each (machine_id, process_id) pair has a 'start' and 'end' timestamp.


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.
*/

WITH end_process AS (
SELECT
tmp.machine_id,
tmp.process_id,
tmp.timestamp AS end_timestamp
FROM Activity AS tmp
WHERE tmp.activity_type = 'end'
)

SELECT
a.machine_id,
ROUND(AVG(e.end_timestamp - a.timestamp)::numeric, 3) AS processing_time
FROM Activity AS a
LEFT JOIN
end_process AS e
ON
a.machine_id = e.machine_id
AND a.process_id = e.process_id
WHERE a.activity_type = 'start'
GROUP BY a.machine_id