Day 9 | SQL LeetCode Problems | Join | Sub-Query | Average Time of Process per Machine

Опубликовано: 06 Январь 2025
на канале: Your Analyst
106
7

This particular problem can be found on Leetcode SQL study plan
The link for the same is added here:-
Average time of process per machine
https://leetcode.com/problems/average...

Link for the study plan:
https://leetcode.com/studyplan/top-sq...


In case you want to check the previous videos, you can check here:-
Day 8:    • Day 8 of 30 day | LeetCode Problem Ri...  
Day 7:    • Day 7 | SQL interview Questions| | Le...  
Day 6:    • Day 6 | SQL Interview Questions: SQL ...  
Day 5:    • Day 5 | SQL Interview Question | Find...  
Day 4:    • Day 4 | SQL Interview Question | Leet...  
Day 3:    • Day 3 | SQL interview Question | Leet...  
Day 2:    • Day 2 Mastering SQL Queries | LeetCod...  
Day 1:    • Day 1: Mastering SQL Queries | LeetCo...  

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.