[ORACLE_SQL](Leet_Code)1661. Average Time of Process per Machine(X)

이경영·2023년 8월 11일
0

오라클

목록 보기
39/43

https://leetcode.com/problems/average-time-of-process-per-machine/description/?envType=study-plan-v2&envId=top-sql-50


문제요약

각 머신의 끝-시작의 시간을 구한뒤 각 머신의 process_id의 횟수만큼 나눈다.

포인트

셀프조인, 그룹바이

  1. 셀프조인을 생각을 못했다. 보통 이렇게 위아래로 더하거나 빼는 문장들은 partition by 를 쓰거나
    셀프조인으로 row개수*row개수 만큼의 데이터를 뽑은뒤에 on절로 필요없는 부분을 필터링하고 사용하는것 같다.
select * from Activity a inner join Activity b
on a.machine_id=b.machine_id 
and a.process_id = b.process_id 
and a.timestamp < b.timestamp 

  1. 이후에 machineid를 뽑아내고 b의 activity_type에서 a의 activity type을 뺀뒤에 avg를 구한다.

select a.machine_id, round(avg(b.timestamp-a.timestamp),3) as processing_time from Activity a inner join Activity b
on a.machine_id=b.machine_id 
and a.process_id = b.process_id 
and a.timestamp < b.timestamp 
group by a.machine_id
order by a.machine_id
profile
꾸준히

0개의 댓글