반응형
https://leetcode.com/problems/average-time-of-process-per-machine/
with절을 사용해본 문제였습니다.
📕 풀이방법
📔 풀이과정
1. machin_id, process_id에 대한 group by로 activity_type의 start와 end 시간을 뺀 processing_time을 projection해 ProcessTime table에 정리합니다.
2. 이 ProcessTime의 processing_time을 평균값으로 projection해 MachineAverageTimes table에 저장합니다.
3. MachineAverageTimes의 모든 column을 select합니다.
📕 Code
📔 SQL
with ProcessTime as (
select
machine_id,
max(case when activity_type = 'end' then timestamp else null end) -
max(case when activity_type = 'start' then timestamp else null end) as processing_time
from activity a
group by machine_id, process_id
),
MachineAverageTimes as (
select
machine_id,
round(avg(processing_time), 3) as processing_time
from ProcessTime
group by machine_id
)
select machine_id, processing_time from MachineAverageTimes
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'SQL' 카테고리의 다른 글
(SQL) - LeetCode (easy) 1683. Invalid Tweets (0) | 2024.06.07 |
---|---|
(SQL) - LeetCode (easy) 1667. Fix Names in a Table (0) | 2024.05.30 |
(SQL) - LeetCode (easy) 1587. Bank Account Summary II (0) | 2024.05.02 |
(SQL) - LeetCode (easy) 1581. Customer Who Visited but Did Not Make Any Transactions (0) | 2024.04.30 |
(SQL) - LeetCode (easy) 1407. Top Travellers (0) | 2024.03.12 |