返回介绍

solution / 1600-1699 / 1661.Average Time of Process per Machine / README_EN

发布于 2024-06-17 01:03:16 字数 5085 浏览 0 评论 0 收藏 0

1661. Average Time of Process per Machine

中文文档

Description

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.

 

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 1:

Input: 
Activity table:
+------------+------------+---------------+-----------+
| machine_id | process_id | activity_type | timestamp |
+------------+------------+---------------+-----------+
| 0      | 0      | start     | 0.712   |
| 0      | 0      | end       | 1.520   |
| 0      | 1      | start     | 3.140   |
| 0      | 1      | end       | 4.120   |
| 1      | 0      | start     | 0.550   |
| 1      | 0      | end       | 1.550   |
| 1      | 1      | start     | 0.430   |
| 1      | 1      | end       | 1.420   |
| 2      | 0      | start     | 4.100   |
| 2      | 0      | end       | 4.512   |
| 2      | 1      | start     | 2.500   |
| 2      | 1      | end       | 5.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.520 - 0.712) + (4.120 - 3.140)) / 2 = 0.894
Machine 1's average time is ((1.550 - 0.550) + (1.420 - 0.430)) / 2 = 0.995
Machine 2's average time is ((4.512 - 4.100) + (5.000 - 2.500)) / 2 = 1.456

Solutions

Solution 1: Grouping and Aggregation

We can group by machine_id and use the AVG function to calculate the average time consumption of all process tasks on each machine. Since each process task on the machine has a pair of start and end timestamps, the time consumption of each process task can be calculated by subtracting the start timestamp from the end timestamp. Therefore, we can use the CASE WHEN or IF function to calculate the time consumption of each process task, and then use the AVG function to calculate the average time consumption of all process tasks on each machine.

Note that each machine has $2$ process tasks, so we need to multiply the calculated average time consumption by $2$.

# Write your MySQL query statement below
SELECT
  machine_id,
  ROUND(
    AVG(
      CASE
        WHEN activity_type = 'start' THEN -timestamp
        ELSE timestamp
      END
    ) * 2,
    3
  ) AS processing_time
FROM Activity
GROUP BY 1;

Solution 2

# Write your MySQL query statement below
SELECT
  machine_id,
  ROUND(AVG(IF(activity_type = 'start', -1, 1) * timestamp) * 2, 3) AS processing_time
FROM Activity
GROUP BY 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文