From 56f28199d3b59197fbc5f1fa779fe34e0869cc77 Mon Sep 17 00:00:00 2001 From: ivanbyone Date: Tue, 8 Jul 2025 16:53:20 +0300 Subject: [PATCH] task: #1661 --- README.md | 1 + ...1. Average Time of Process per Machine.sql | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 leetcode/easy/1661. Average Time of Process per Machine.sql diff --git a/README.md b/README.md index 6b9e040..d0bdfb3 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/leetcode/easy/1661. Average Time of Process per Machine.sql b/leetcode/easy/1661. Average Time of Process per Machine.sql new file mode 100644 index 0000000..eae5273 --- /dev/null +++ b/leetcode/easy/1661. Average Time of Process per Machine.sql @@ -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