안녕하세요!
이번 포스팅은 LeetCode에 있는 Average Time of Process per Machine 문제를 OracleDB로 풀어보려고 합니다!
(모든 문제는 Oracle로 풀이하겠습니다.)
1. 문제 링크 : https://leetcode.com/problems/average-time-of-process-per-machine/description/
2. 문제
| Column name | Type |
| machine_id | int |
| process_id | int |
| activity_type | enum |
| timestamp | float |
[문제] 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.
machine_id와 함께 processing_time이라는 평균 시간을 조회하는 문제입니다.
3. 제출 쿼리 및 설명
1) ACTIVITY_TYPE이 'start'이면 timestamp 값을 음수로 바꾼 조정된 TIMESTAMP의 값을 구합니다.
SELECT MACHINE_ID, PROCESS_ID,
CASE WHEN ACTIVITY_TYPE = 'start' THEN -timestamp
ELSE TIMESTAMP END AS ADJ_TIMESTAMP
FROM ACTIVITY
최종) 조정된 TIMESTAMP(ADJ_TIMESTAMP)가 있는 1번 쿼리를 인라인 뷰로 활용해서 MACHINE_ID 별로 평균 시간을 조회합니다.
SELECT MACHINE_ID, ROUND(SUM(ADJ_TIMESTAMP)/COUNT(DISTINCT PROCESS_ID), 3) AS PROCESSING_TIME
FROM (
SELECT MACHINE_ID, PROCESS_ID,
CASE WHEN ACTIVITY_TYPE = 'start' THEN -timestamp
ELSE TIMESTAMP END AS ADJ_TIMESTAMP
FROM ACTIVITY
)
GROUP BY MACHINE_ID
;
'SQL > LeetCode' 카테고리의 다른 글
| [SQL] Invalid Tweets(LeetCode/Oracle) (0) | 2023.10.26 |
|---|---|
| [SQL] Fix Names in a Table(LeetCode/Oracle) (0) | 2023.10.26 |
| [SQL] Percentage of Users Attended a Contest(LeetCode/Oracle) (0) | 2023.10.26 |
| [SQL] Bank Account Summary II(LeetCode/Oracle) (0) | 2023.10.26 |
| [SQL] Patients With a Condition(LeetCode/Oracle) (0) | 2023.10.26 |