LeetCode - 1661. Average Time of Process per Machine (MySQL)

조민수·2024년 6월 16일
0

LeetCode

목록 보기
37/64
post-custom-banner

Easy, SQL - 자기 자신 참조

RunTime : 220 ms


문제

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.


풀이

  • 같은 machine_id & process_id 의 시작 시점과 끝 시점을 찾아야했던 문제
    - 뭔가 자기 참조로 만들어내는건 Easy도 어렵다.
SELECT a.machine_id, ROUND(AVG(b.timestamp - a.timestamp),3) as processing_time
FROM Activity as a JOIN Activity as b
ON a.process_id = b.process_id and a.machine_id = b.machine_id
where a.activity_type = 'start' and b.activity_type = 'end'
GROUP BY 1
profile
사람을 좋아하는 Front-End 개발자
post-custom-banner

0개의 댓글