【发布时间】:2021-07-08 00:47:56
【问题描述】:
我有一个由employee_id、start_date、end_date、hour_spent_per_day 组成的数据框。基本上对于每条记录,它都会记录员工每天花费的时间以及 start_Date 和 end_Date 给定的持续时间。
| employee_id | start_Date | end_Date | hours_spent_per_day |
|---|---|---|---|
| 1234 | 2019-01-15 | 2019-01-15 | 2 |
| 1234 | 2019-01-15 | 2019-01-17 | 3 |
| 1236 | 2019-01-29 | 2019-02-28 | 4 |
| 1237 | 2019-01-19 | 2019-03-05 | 2 |
| 1237 | 2019-12-01 | 2020-01-10 | 2 |
我的目标是汇总每年每月花费的总小时数。 即
| Year | Month | Hours Spent |
|---|---|---|
| 2019 | January | 'hours spent for all the days in Jan-2019 for all employees |
| 2019 | February | 'hours spent for all the days in Feb-2019 for all employees |
| 2019 | March | 'hours spent for all the days in Mar-2019 for all employees |
| 2019 | December | 'hours spent for all the days in Dec-2019 for all employees |
| 2020 | January | 'hours spent for all the days in Jan-2020 for all employees |
现在第二个表很容易(只是按 SQL 分组的问题)。但是我对第一个表有问题,即对于每一行,我如何确定每个月花费的时间(注意跨月是可能的)。我想我需要确定每一行所涵盖的确切月份/年份,然后乘以该月花费的时间。我被困住了,寻求您的帮助。
【问题讨论】: