Skip to content

1661. Average Time of Process per Machine

Problem Statement

Table: Activity

Column NameType
machine_idint
process_idint
activity_typeenum
timestampfloat

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.

Instructions

  • 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.
  • The result format is in the following example.

Example

Input: Activity table

machine_idprocess_idactivity_typetimestamp
00start0.712
00end1.520
01start3.140
01end4.120
10start0.550
10end1.550
11start0.430
11end1.420
20start4.100
20end4.512
21start2.500
21end5.000

Output:

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

Explanation:

There are 3 machines running 2 processes each.

Machine 0's average time is (1.5200.712)+(4.1203.140)2 = 0.894

Machine 1's average time is ((1.5500.550)+(1.4200.430))2 = 0.995

Machine 2's average time is ((4.5124.100)+(5.0002.500))2 = 1.456

Submissions

sql
SELECT
    machine_id,
    ROUND(AVG(end_time - start_time)::numeric, 3) AS processing_time
FROM (
    SELECT
        machine_id,
        process_id,
        MAX(CASE WHEN activity_type = 'end' THEN timestamp END) AS end_time,
        MIN(CASE WHEN activity_type = 'start' THEN timestamp END) AS start_time
    FROM Activity
    GROUP BY machine_id, process_id
) AS process_times
GROUP BY machine_id;

Explanations

PostgreSQL
Submitted by @noeyislearning
  • SELECT machine_id: Select the machine_id from the Activity table.
  • ROUND(AVG(end_time - start_time)::numeric, 3) AS processing_time: Calculate the average time it takes to complete a process by subtracting the 'start' timestamp from the 'end' timestamp and rounding it to 3 decimal places.
  • FROM (SELECT machine_id, process_id, MAX(CASE WHEN activity_type = 'end' THEN timestamp END) AS end_time, MIN(CASE WHEN activity_type = 'start' THEN timestamp END) AS start_time FROM Activity GROUP BY machine_id, process_id) AS process_times: Select the machine_id, process_id, 'end' timestamp, and 'start' timestamp from the Activity table and group them by machine_id and process_id.
  • GROUP BY machine_id: Group the results by machine_id.