求和特定日期的多个时差

求和特定日期的多个时差

我有这个问题

SELECT Date_Format(te.entry_datetime, '%Y-%m-%d') as Date,
       te2.entry_datetime as Time_in,
       te3.entry_datetime as Time_out,

       TIMEDIFF(te7.entry_datetime, te6.entry_datetime) as Total_break

FROM time_entries te

JOIN time_entries te2 ON te.employee_index = te2.employee_index
AND te2.entry_type_index='1' AND te2.entry_datetime =
   (SELECT entry_datetime
    FROM time_entries
    WHERE entry_type_index='1' AND employee_index = 22 AND entry_datetime = te.entry_datetime
    GROUP BY entry_datetime)

LEFT JOIN time_entries te3 ON te.employee_index = te3.employee_index
AND te3.entry_type_index='2' AND te3.entry_datetime =
    (SELECT entry_datetime
    FROM time_entries
    WHERE entry_type_index='2' AND employee_index = 22 AND entry_datetime > te.entry_datetime
    ORDER BY
    ABS(TIMESTAMPDIFF(SECOND, te.entry_datetime, 'entry_datetime'))
    LIMIT 1)


LEFT JOIN time_entries te6 ON te.employee_index = te6.employee_index
AND te6.entry_type_index='6' AND te6.entry_datetime =
    (SELECT entry_datetime
    FROM time_entries
    WHERE entry_type_index='6' AND employee_index = 22 AND entry_datetime > te.entry_datetime
    ORDER BY
    ABS(TIMESTAMPDIFF(SECOND, te.entry_datetime, 'entry_datetime'))
    LIMIT 1)


LEFT JOIN time_entries te7 ON te.employee_index = te7.employee_index
AND te7.entry_type_index='7' AND te7.entry_datetime =
   (SELECT entry_datetime
   FROM time_entries
   WHERE entry_type_index='7' AND employee_index = 22 AND entry_datetime > te.entry_datetime
   ORDER BY
   ABS(TIMESTAMPDIFF(SECOND, te.entry_datetime, 'entry_datetime'))
   LIMIT 1)

这就是结果
|   Date     |        Time_in      |      Time_out       | Total_break |
| 2013-08-23 | 2013-08-30 09:00:00 | 2013-08-30 19:01:00 | 00:36:03    |

除了一个问题外,查询工作正常。总中断时间仅与一个中断时间之差相加(条目类型7是中断结束,6是中断开始)。我想总结一下某一天休息时间的所有差异(每个条目的索引7和6)。
这是我期望的答案
|   Date     |        Time_in      |      Time_out       | Total_break |
| 2013-08-23 | 2013-08-30 09:00:00 | 2013-08-30 19:01:00 | 00:59:04    |

求出条目类型索引2和条目类型索引1之间每个条目类型索引7和6的差值,然后将它们相加。
这是来自我的表的一个示例输出(time_entries)
|   employee_index  |    entry_type_index   |   entry_datetime    |
|       22          |           2           | 2013-08-30 19:01:00 |
|       22          |           7           | 2013-08-30 16:29:07 |
|       22          |           6           | 2013-08-30 16:06:06 |
|       22          |           7           | 2013-08-30 15:40:06 |
|       22          |           6           | 2013-08-30 15:04:03 |
|       22          |           1           | 2013-08-30 09:00:00 |

感谢那些能指导我做这件事的人。

最佳答案

select
x.employee_index,
max(case when x.entry_type_index = 1 then x.entry_datetime else null end) as in_time,
max(case when x.entry_type_index = 2 then x.entry_datetime else null end) as out_time,
sum(case when x.entry_type_index = 6 then y.entry_datetime-x.entry_datetime else 0 end as break
from (select @rn:=@rn+1 as rn, a.*
from table1 a,
(select @rn := 0) b
 order by entry_datetime) x left join
(select @rn1:=@rn1+1 as rn1, a.*
from table1 a,
(select @rn1 := -1) b
 order by entry_datetime) y
on x.rn = y.rn1
group by x.employee_index

关于php - 求和特定日期的多个时差,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18507564/

10-11 01:35