+-------------+------+
|Column Name |Type|+-------------+------+
| emp_id | int || event_day | date || in_time | int || out_time | int |+-------------+------+
(emp_id, event_day, in_time) is the primary key (combinations of columns with unique values) of this table.
The table shows the employees’ entries and exits in an office.
event_day is the day at which this event happened, in_time is the minute at which the employee entered the office, and out_time is the minute at which they left the office.
in_time and out_time are between 1 and 1440.
It is guaranteed that no two events on the same day intersect in time, and in_time < out_time.
Write a solution to calculate the total time in minutes spent by each employee on each day at the office. Note that within one day, an employee can enter and leave more than once. The time spent in the office for a single entry is out_time - in_time.
Explanation:
Employee 1 has three events: two on day 2020-11-28 with a total of (32 - 4) + (200 - 55) = 173, and one on day 2020-12-03 with a total of (42 - 1) = 41.
Employee 2 has two events: one on day 2020-11-28 with a total of (33 - 3) = 30, and one on day 2020-12-09 with a total of (74 - 47) = 27.
select event_day asday, emp_id, sum(out_time - in_time) as total_time
from Employees
groupby event_day, emp_id
1
2
3
4
5
6
7
select event_day asday, emp_id
, sum(out_time) -sum(in_time) as total_time
from Employees
groupby event_day, emp_id
1
2
3
4
5
6
7
8
9
10
11
import pandas as pd
deftotal_time(employees: pd.DataFrame) -> pd.DataFrame:
# Calculate the time spent for each entry employees['time_spent'] = employees['out_time'] - employees['in_time']
# Group by emp_id and event_day, then sum the time_spent result_df = employees.groupby(['emp_id', 'event_day'])['time_spent'].sum().reset_index()
result_df.rename(columns={'event_day': 'day', 'time_spent': 'total_time'}, inplace=True)
return result_df