susinlee 님의 블로그

Average Time of Process per Machine 본문

코드카타/SQL, Pandas

Average Time of Process per Machine

susinlee 2024. 12. 21. 12:30

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.

 

[풀이]

1. start와 end를 열로 분리해서 보면 좋을 것 같으니 pivot_table을 만들자

pivoted = activity.pivot(index=['machine_id', 'process_id'], columns='activity_type', values='timestamp')

 

 

2. 작업시간을 구해야 하니 'end' 컬럼에서 'start' 컬럼을 뺀 값을 새로운 컬럼('processing_time')으로 만들어보자

pivoted['processing_time'] = pivoted['end'] - pivoted['start']

 

 

3. machine_id 별로 processing_time의 평균을 구하면 끝

pivoted.groupby('machine_id')['processing_time'].mean().round(3).reset_index()

 

전체 코드

import pandas as pd

def get_average_time(activity: pd.DataFrame) -> pd.DataFrame:
    pivoted = activity.pivot(index=['machine_id', 'process_id'], columns='activity_type', values='timestamp')
    pivoted['processing_time'] = pivoted['end'] - pivoted['start']

    return pivoted.groupby('machine_id')['processing_time'].mean().round(3).reset_index()

 

SQL

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 a1.machine_id

-- SELECT
--     machine_id,
--     ROUND(
--     (SELECT AVG(timestamp) FROM Activity a1 WHERE activity_type = 'end' AND a1.machine_id = a.machine_id) - 
--     (SELECT AVG(timestamp) FROM Activity a1 WHERE activity_type = 'start' AND a1.machine_id = a.machine_id)
--     , 3) AS processing_time
-- FROM Activity a
-- GROUP BY machine_id

 

'코드카타 > SQL, Pandas' 카테고리의 다른 글

Students and Examinations  (0) 2024.12.22
Employee Bonus  (0) 2024.12.22
Rising Temperature  (0) 2024.12.21
Customer Who Visited but Did Not Make Any Transcations  (1) 2024.12.20
Product Sales Analysis 1  (0) 2024.12.20