I have one table.a
with multiple columns, but only three are important for the case, lets name them time_stamp
, employee_number
and station_id
.
I got this query:
SELECT checktime AS time_stamp, userid AS employee_number, SN AS station_id
FROM checkinout;
to get this information:
+---------------------+-----------------+---------------+
| time_stamp | employee_number | station_id |
+---------------------+-----------------+---------------+
| 30/05/2015 8:01:07 | 5 | 6068144700170 |
| 31/05/2015 8:05:17 | 2 | 6068144700170 |
| 31/05/2015 17:03:47 | 2 | 6068144700170 |
| 31/05/2015 22:03:24 | 13 | 6068144700170 |
| 01/06/2015 02:30:34 | 13 | 6068144700170 |
| 01/06/2015 03:24:33 | 13 | 6068144700170 |
| 01/06/2015 07:14:24 | 13 | 6068144700170 |
| ... | ... | ... |
+---------------------+-----------------+---------------+
And what I need is to classify type
and status
of time_stamp
by sequence for each employee_number
on every 20 hours interval. Why not each day? Because some employees start labor at night and finish other day morning.
The classification should be made by time_stamp
count, like in case of 1 timestamp for employee.a then t=0/s=I, case of 2 timestamps for employee.a then t=0/s=I - t=0/s=O, case of 3 timestamps for employee.a then t=0/s=I - t=2/s=O - t=0/s=O, case of 4 timestamps for employee.a then t=0/s=I - t=2/s=O - t=2/s=I - t=0/s=O...
Example of needed output:
+---------------------+-----------------+---------------+---------+-----------+
| time_stamp | employee_number | station_id | type_id | status_id |
+---------------------+-----------------+---------------+---------+-----------+
| 30/05/2015 8:01:07 | 5 | 6068144700170 | 0 | I |
| 31/05/2015 8:05:17 | 2 | 6068144700170 | 0 | I |
| 31/05/2015 17:03:47 | 2 | 6068144700170 | 0 | O |
| 31/05/2015 22:03:24 | 13 | 6068144700170 | 0 | I |
| 01/06/2015 02:30:34 | 13 | 6068144700170 | 2 | O |
| 01/06/2015 03:24:33 | 13 | 6068144700170 | 2 | I |
| 01/06/2015 07:14:24 | 13 | 6068144700170 | 0 | O |
| ... | ... | ... | ... | ... |
+---------------------+-----------------+---------------+---------+-----------+
This questions help me, but did't solved entirely the issue:
MySQL: Get start & end timestamp for each day
Mysql Show records where timestamp interval is lower then 4 minutes
Solved! Based on this one: MySQL - Subtracting value from previous row, group by
CODE:
It retrieves three additional columns (userid_test, timestamp_test and diference) but for me is fine, as I got type and status for each timestamp based on previous timestamp and time interval.