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 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)