Grokking SQL for Tech Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
17. Average Time of Process
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem

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.

Problem Definition

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.

Example

Image

Output

Image

Try It Yourself

MYSQL
MYSQL

. . . .

Solution

To solve this problem, the approach involves using SQL queries to calculate the average time each machine takes to complete a process based on the given Activity table. The table contains information about machine activities, including the machine ID, process ID, activity type ('start' or 'end'), and a timestamp indicating when the activity occurred.

The solution utilizes the JOIN clause to combine the Activity table (a1) with itself (a2). The ON clause specifies the conditions for joining, including that both activities should have the same machine_id, process_id, and the first activity (a1) should be of type 'start', while the second activity (a2) should be of type 'end'. This ensures that the start and end activities for the same process on the same machine are paired together.

The calculation of the time taken to complete a process is achieved by subtracting the timestamp of the 'start' activity from the timestamp of the 'end' activity. The Round function is then applied to round the average processing time to three decimal places.

The results are grouped by machine_id using the GROUP BY clause, ensuring that the calculations are performed for each unique machine. The final query provides a clear and organized result table with machine_id and the calculated average processing time.

SELECT a1.machine_id, Round(Avg(a2.timestamp - a1.timestamp), 3) AS processing_time FROM Activity a1 JOIN Activity a2 ON 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

Let's break down the query step by step:

Step 1: Joining the Activity table with itself

We are joining the Activity table with itself, aliased as a1 and a2, to match the start and end activities for each (machine_id, process_id) pair.

SELECT a1.machine_id, Round(Avg(a2.timestamp - a1.timestamp), 3) AS processing_time FROM Activity a1 JOIN Activity a2 ON a1.machine_id = a2.machine_id AND a1.process_id = a2.process_id AND a1.activity_type = 'start' AND a2.activity_type = 'end'

Output After Step 1:

+------------+-----------------+ | machine_id | processing_time | +------------+-----------------+ | 0 | ... | | 1 | ... | | 2 | ... | +------------+-----------------+

Step 2: Grouping by machine_id

We group the results by machine_id to calculate the average processing time for each machine.

GROUP BY a1.machine_id

Output After Step 2:

+------------+-------------------+ | machine_id | processing_time | +------------+-------------------+ | 0 | 0.89478 | | 1 | 0.99538 | | 2 | 1.45656 | +------------+-------------------+

Now, let's calculate the average processing time for each machine_id:

Step 3: Calculate average processing time

We use the AVG function to calculate the average processing time, and ROUND to round it to 3 decimal places.

Round(Avg(a2.timestamp - a1.timestamp), 3) AS processing_time

Final Output:

+------------+-----------------+ | machine_id | processing_time | +------------+-----------------+ | 0 | 0.894 | | 1 | 0.995 | | 2 | 1.456 | +------------+-----------------+

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible