douhuan1979 2015-07-03 12:39 采纳率: 100%
浏览 60
已采纳

合并sql表中的重叠日期间隔

I have a table which contains shift sign in and sign out times of employees. Each record has AutoGenID, employeeID, start datetime and end datetime. Based on these data i want to find the hours of work for each employee for a given period of time such as a month.

This is not web based but a Mobile and a PC app which can run in offline mode.

The problem is that there may or may not be multiple records for a single employee with overlapping date intervals. This is because an employee can individually sigh in on multiple devices per day and all these entries are added on this centralized database table. Hence there may or may not be overlapping date intervals for a single employee.

For example lets say that user A signed in from his mobile at '2015-07-03 10:51:19' and later signed in again from his PC at '2015-07-03 12:36:14'

At the end of the day he signed off from his PC at '2015-07-03 18:12:29' and signed off his mobile at '2015-07-03 18:19:53'

And Next day he only uses his PC to sign in. So on the database i have the following records.

+----+-----------+---------------------+---------------------+
| ID |   EmpID   |         start       |         End         |
+----+-----------+---------------------+---------------------+
| 1  |   EM001   | 2015-07-03 10:51:19 | 2015-07-03 18:19:53 |
| 2  |   EM001   | 2015-07-03 12:36:14 | 2015-07-03 18:12:29 |
| 3  |   EM001   | 2015-07-04 11:34:52 | 2015-07-04 17:21:43 |
+----+-----------+---------------------+---------------------+

But when querying the data i only need the Hours the employee worked. So I need the first start time and the last end time if the dates are overlapping so that i can calculate the hours. Again this issue is there only for days with overlapping times and there may not be overlapping times for a single employee on some days.

This hour calculation is not for employee salary purposes. Its just to capture the work hour of the user.

So does anyone have any idea on how to do this.

Thanks

  • 写回答

1条回答 默认 最新

  • douba9020 2015-07-03 12:57
    关注

    Dipen Shah has a point, but sometimes you can't change the way sign-ins are logged. In this case perhaps you can try grouping the table by the date and querying the min(start) and max (end) of every user and date, like this:

    select empid, min(start), max(end) from signin group by empid, date(start);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮