링크
https://leetcode.com/problems/average-time-of-process-per-machine/description/
문제
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.
정답
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 1
새롭게 이해한 내용
- 같은 테이블 내 비교할 때는 한 테이블을 a1, a2로 나눠 조인 후 분할
- on절에 조건을 걸어 a1에 한 경우만 출력하고 a2에 한 경우만 출력
'[업무 지식] > MySQL' 카테고리의 다른 글
[확인 비율 구하기] Confirmation Rate (0) | 2024.10.28 |
---|---|
[Cross Join] Students and Examinations (0) | 2024.10.25 |
[on 조건] Rising Temperature (0) | 2024.10.24 |
[NULL] Find Customer Referee (0) | 2024.10.23 |
상품을 구매한 회원 비율 구하기 (0) | 2024.10.22 |