|
| 1 | +-- Table: Activity |
| 2 | + |
| 3 | +-- +----------------+---------+ |
| 4 | +-- | Column Name | Type | |
| 5 | +-- +----------------+---------+ |
| 6 | +-- | machine_id | int | |
| 7 | +-- | process_id | int | |
| 8 | +-- | activity_type | enum | |
| 9 | +-- | timestamp | float | |
| 10 | +-- +----------------+---------+ |
| 11 | +-- The table shows the user activities for a factory website. |
| 12 | +-- (machine_id, process_id, activity_type) is the primary key (combination of columns with unique values) of this table. |
| 13 | +-- machine_id is the ID of a machine. |
| 14 | +-- process_id is the ID of a process running on the machine with ID machine_id. |
| 15 | +-- activity_type is an ENUM (category) of type ('start', 'end'). |
| 16 | +-- timestamp is a float representing the current time in seconds. |
| 17 | +-- 'start' means the machine starts the process at the given timestamp and 'end' means the machine ends the process at the given timestamp. |
| 18 | +-- The 'start' timestamp will always be before the 'end' timestamp for every (machine_id, process_id) pair. |
| 19 | + |
| 20 | + |
| 21 | +-- 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. |
| 22 | + |
| 23 | +-- 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. |
| 24 | + |
| 25 | +-- The resulting table should have the machine_id along with the average time as processing_time, which should be rounded to 3 decimal places. |
| 26 | + |
| 27 | +-- Return the result table in any order. |
| 28 | + |
| 29 | +-- The result format is in the following example. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +-- Example 1: |
| 34 | + |
| 35 | +-- Input: |
| 36 | +-- Activity table: |
| 37 | +-- +------------+------------+---------------+-----------+ |
| 38 | +-- | machine_id | process_id | activity_type | timestamp | |
| 39 | +-- +------------+------------+---------------+-----------+ |
| 40 | +-- | 0 | 0 | start | 0.712 | |
| 41 | +-- | 0 | 0 | end | 1.520 | |
| 42 | +-- | 0 | 1 | start | 3.140 | |
| 43 | +-- | 0 | 1 | end | 4.120 | |
| 44 | +-- | 1 | 0 | start | 0.550 | |
| 45 | +-- | 1 | 0 | end | 1.550 | |
| 46 | +-- | 1 | 1 | start | 0.430 | |
| 47 | +-- | 1 | 1 | end | 1.420 | |
| 48 | +-- | 2 | 0 | start | 4.100 | |
| 49 | +-- | 2 | 0 | end | 4.512 | |
| 50 | +-- | 2 | 1 | start | 2.500 | |
| 51 | +-- | 2 | 1 | end | 5.000 | |
| 52 | +-- +------------+------------+---------------+-----------+ |
| 53 | +-- Output: |
| 54 | +-- +------------+-----------------+ |
| 55 | +-- | machine_id | processing_time | |
| 56 | +-- +------------+-----------------+ |
| 57 | +-- | 0 | 0.894 | |
| 58 | +-- | 1 | 0.995 | |
| 59 | +-- | 2 | 1.456 | |
| 60 | +-- +------------+-----------------+ |
| 61 | +-- Explanation: |
| 62 | +-- There are 3 machines running 2 processes each. |
| 63 | +-- Machine 0's average time is ((1.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894 |
| 64 | +-- Machine 1's average time is ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995 |
| 65 | +-- Machine 2's average time is ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456 |
| 66 | + |
| 67 | + |
| 68 | +-- Write your PostgreSQL query statement below |
| 69 | +-- Solution |
| 70 | +select machine_id, |
| 71 | + ROUND(CAST("processing_time" AS NUMERIC), 3) AS processing_time |
| 72 | +from |
| 73 | +( |
| 74 | + select X.machine_id machine_id, |
| 75 | + X.total_completion_time/Y.num_processes as processing_time |
| 76 | + from |
| 77 | + ( |
| 78 | + select transformed.machine_id as machine_id, |
| 79 | + sum(transformed.process_completion_time) as total_completion_time |
| 80 | + from |
| 81 | + ( |
| 82 | + select A.machine_id, |
| 83 | + A.process_id, |
| 84 | + A.activity_type A_atype, |
| 85 | + A.timestamp A_tstamp, |
| 86 | + B.activity_type B_atype, |
| 87 | + B.timestamp B_tstamp, |
| 88 | + B.timestamp - A.timestamp as process_completion_time |
| 89 | + from Activity A |
| 90 | + join Activity B |
| 91 | + on (A.machine_id = B.machine_id |
| 92 | + and A.process_id = B.process_id) |
| 93 | + where (A.timestamp < B.timestamp |
| 94 | + and A.activity_type = 'start' |
| 95 | + and B.activity_type = 'end') |
| 96 | + ) transformed |
| 97 | + group by transformed.machine_id |
| 98 | + ) |
| 99 | + X |
| 100 | + join |
| 101 | + ( |
| 102 | + select machine_id, |
| 103 | + count(process_id) as num_processes |
| 104 | + from (select distinct machine_id, process_id from Activity) |
| 105 | + group by machine_id |
| 106 | + ) |
| 107 | + Y on X.machine_id = Y.machine_id |
| 108 | +) |
0 commit comments