Leecode_SQL50_1661. Average Time of Process per Machine

Leecode_SQL50_1661. Average Time of Process per Machine

Problem 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.

It is guaranteed that each (machine_id, process_id) pair has a 'start' and 'end' timestamp.

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

My solution

sql 复制代码
WITH st AS (
    SELECT machine_id, process_id, timestamp
    FROM Activity
    WHERE activity_type = 'start'
), 
en AS (
    SELECT machine_id, process_id, timestamp
    FROM Activity
    WHERE activity_type = 'end'
), 
b AS (
    SELECT st.machine_id, st.process_id, (en.timestamp - st.timestamp) AS time_taken
    FROM st 
        JOIN en 
            ON st.machine_id = en.machine_id 
            AND st.process_id = en.process_id
)

SELECT b.machine_id, ROUND(AVG(time_taken), 3) AS processing_time
FROM b
GROUP BY b.machine_id

Other solutions

sql 复制代码
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
sql 复制代码
Select a.machine_id,ROUND(AVG(b.timestamp-a.timestamp), 3)as processing_time 
from Activity a, Activity b
where a.process_id=b.process_id 
	AND a.machine_id=b.machine_id
	AND a.activity_type="start" 
	AND b.activity_type="end"
group by machine_id;
相关推荐
时差95310 分钟前
【面试题】Hive 查询:如何查找用户连续三天登录的记录
大数据·数据库·hive·sql·面试·database
让学习成为一种生活方式12 分钟前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
Mephisto.java18 分钟前
【大数据学习 | kafka高级部分】kafka的优化参数整理
大数据·sql·oracle·kafka·json·database
秋意钟39 分钟前
MySQL日期类型选择建议
数据库·mysql
山海青风1 小时前
第七篇: BigQuery中的复杂SQL查询
sql·googlecloud
Dxy12393102161 小时前
python下载pdf
数据库·python·pdf
桀桀桀桀桀桀2 小时前
数据库中的用户管理和权限管理
数据库·mysql
lzhlizihang3 小时前
【Hive sql 面试题】求出各类型专利top 10申请人,以及对应的专利申请数(难)
大数据·hive·sql·面试题
superman超哥3 小时前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
用户8007165452003 小时前
HTAP数据库国产化改造技术可行性方案分析
数据库