1

このクエリがあります

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    |

1 つの問題を除いて、クエリは正常に動作しています。Total_break は、1 つの休憩時間の差のみを合計します (entry_type_index 7 は break_end であり、6 は break_start です)。特定の日の休憩時間 (entry_type_index 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    |

entry_type_index 2 と 1 の間のすべての entry_type_index 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 | 

そうするように私を導いてくれた人々に感謝します。

4

1 に答える 1

1
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
于 2013-08-29T11:01:19.733 に答える