Reputation: 5805
Here is the problem that I need to solve:
This is the table that I need to get for each user.
Date | Working time (day) | Night shifts (23:00 - 06:00) | Total(day + night) | | Notes
3.9. | 7 | 2 | 9 | 1 |
4.9. | 8 | 0 | 8 | |
5.9. | 6 | 2 | 8 | |
6.9. | 4 | 3 | 7 | |
7.9. | 0 | 0 | 0 | |
8.9. | 0 | 0 | 0 | |
9.9. | 0 | 0 | 0 | |
10.9. | 0 | 0 | 0 | |
Total(week) | 25 | 7 | 32 | Total(>48h)| 0
So let me explain this. User chooses the week in this example it is a from 3.9-10.9, user has unique key. Everything in this table needs to be generated in sql if this is possible. So the first column is date, second one is a working time which is between (06:00-23:00), third column is night shift between (23:00-06:00), fourth column is Total of day and night shift, fifth column is only filled if fourth column is bigger then 8 i.e. 9h of total - 8 is 1,sixth column is only filled if Total of(day and night shifts) at the bottom is bigger then 48h and then difference is written in that last column.
My current table with data has this columns: user_key,id,time
Now this would be an example of what should query do:
1. user_key(100), id(1), time(15:00);
2. user_key(200), id(2), time(15:05);
3. user_key(100), id(3), time(16:00);
4. user_key(100), id(4), time(16:05);
5. user_key(100), id(5), time(17:05);
3.-1. = 1h
5.-4. = 1h
Result:
user_key(100), day_shift(2h)
So we user_key is unique for each user and id is auto-incrementing, we always need to take first and the second record for the same user and save result and then second two records for that user and save working time and so on... How to achieve this? I need to use some sql variables? Of course it has to have to options if it is a day shift and night shift as we don't want to save working time in the wrong column.
EDIT:
As I see that lot of you didn't understood what I really need. I will try to explain it a bit more.
When user signs in it has it own user_key written. When he signs out then I wrote user_key(0) in the table after that another user can sing in and so on. So basically I need to see first unique user key in the for example day shift and get the last. The last user_key we can get just to check if the next user_key is different. If the next user key is the different then we have the first one and the last one and difference between them needs to be saved in the variable. Then for example we have some other users and later on we have again pair of user_key then we just need to add the difference between them to the same variable and so on...
My problem can be solved in PHP too. For example we can get only values for certain user_key and for the certain time. Then in php we can go trough the data and check.
Please provide me with some examples
EDIT:
Data:
1447, 0, 2012-10-21 13:32:15.453
1448, 0, 2012-10-21 13:32:22.203
1449, 0, 2012-10-21 13:32:29.203
1450, , 2012-10-21 13:32:35.671
1451, , 2012-10-21 13:32:44.515
1452, , 2012-10-21 13:32:52.62
1453, 0010154108, 2012-10-21 13:32:59.203
1454, 0010154108, 2012-10-21 13:33:06.46
1455, 0010154108, 2012-10-21 13:33:13.171
1456, 0010154108, 2012-10-21 13:33:20.62
1457, 0010154108, 2012-10-21 13:33:27.62
1458, 0010154108, 2012-10-21 13:33:34.171
1459, 0010154108, 2012-10-21 13:33:40.234
1460, 0010154108, 2012-10-21 13:33:47
1461, 0010154108, 2012-10-21 13:33:53.812
1462, 0010154108, 2012-10-21 13:33:59.828
1463, 0010154108, 2012-10-21 13:34:05.859
1464, 0010154108, 2012-10-21 13:34:11.828
1465, 0010154108, 2012-10-21 13:34:18.843
1466, 0010154108, 2012-10-21 13:34:24.828
1467, 0010154108, 2012-10-21 13:34:30.781
1468, 0010154108, 2012-10-21 13:34:36.765
1469, 0010154108, 2012-10-21 13:34:43.156
1470, 0010154108, 2012-10-21 13:34:49.375
1471, 0010154108, 2012-10-21 13:34:55.593
1472, 0010154108, 2012-10-21 13:35:03.156
1473, 0010154108, 2012-10-21 13:35:10.875
1474, 0010154108, 2012-10-21 13:35:16.734
1475, 0010154108, 2012-10-21 13:35:24.875
1476, 0010154108, 2012-10-21 13:35:31.750
1477, 0010154108, 2012-10-21 13:35:38.765
1478, 0010154108, 2012-10-21 13:35:45.859
1479, 0010154108, 2012-10-21 13:35:54.203
1480, 0010154108, 2012-10-21 13:36:01.62
1481, 0010154108, 2012-10-21 13:36:08.93
1482, 0010154108, 2012-10-21 13:36:15.109
1483, 0010154108, 2012-10-21 13:36:21.109
1484, 0010154108, 2012-10-21 13:36:28.109
1485, 0010154108, 2012-10-21 13:36:34.93
1486, 0010154108, 2012-10-21 13:36:40.93
1487, 0010154108, 2012-10-21 13:36:46.78
1488, 0010154108, 2012-10-21 13:36:54.921
1489, 0010154108, 2012-10-21 13:37:01.93
1490, 0010154108, 2012-10-21 13:37:08.93
1491, 0010154108, 2012-10-21 13:37:14.78
1492, 0010154108, 2012-10-21 13:37:20.78
1493, 0010154108, 2012-10-21 13:37:26.78
1494, 0010154108, 2012-10-21 13:37:33.93
1495, 0010154108, 2012-10-21 13:37:39.140
1496, 0010154108, 2012-10-21 13:37:45.156
1497, 0010154108, 2012-10-21 13:37:51.171
1498, 0010154108, 2012-10-21 13:37:58.562
1499, 0010154108, 2012-10-21 13:38:05.93
1500, 0010154108, 2012-10-21 13:38:11.125
1501, 0010154108, 2012-10-21 13:38:18.109
1502, 0010154108, 2012-10-21 13:38:24.78
1503, 0010154108, 2012-10-21 13:38:30.15
1504, 0010154108, 2012-10-21 13:38:36.15
1505, 0010154108, 2012-10-21 13:38:42.984
1506, 0010154108, 2012-10-21 13:38:48.984
1507, 0010154108, 2012-10-21 13:38:55
1508, 0010154108, 2012-10-21 13:39:01.750
1509, 0010154108, 2012-10-21 13:39:07.953
1510, 0010154108, 2012-10-21 13:39:13.953
1511, 0010154108, 2012-10-21 13:39:19.984
1512, 0010154108, 2012-10-21 13:39:26
1513, 0010154108, 2012-10-21 13:39:32.984
1514, 0010154108, 2012-10-21 13:39:38.953
1515, 0010154108, 2012-10-21 13:39:44.937
1516, 0010154108, 2012-10-21 13:39:50.937
1517, 0010154108, 2012-10-21 13:39:57.968
1518, 0010154108, 2012-10-21 13:40:03.968
1519, 0010154108, 2012-10-21 13:40:10.15
1520, 0010154108, 2012-10-21 13:40:16.15
1521, 0010154108, 2012-10-21 13:40:22.968
1522, 0010154108, 2012-10-21 13:40:28.953
1523, 0010154108, 2012-10-21 13:40:34.953
1524, 0010154108, 2012-10-21 13:40:40.984
1525, 0010154108, 2012-10-21 13:40:48
1526, 0010154108, 2012-10-21 13:40:54.15
1527, 0010154108, 2012-10-21 13:40:59.984
1528, 0010154108, 2012-10-21 13:41:07.578
1529, 0010154108, 2012-10-21 13:41:13.984
1530, 0010154108, 2012-10-21 13:41:20.31
1531, 0010154108, 2012-10-21 13:41:26.31
1532, 0010154108, 2012-10-21 13:41:33.78
1533, 0010154108, 2012-10-21 13:41:39.46
1534, 0010154108, 2012-10-21 13:41:45.31
1535, 0010154108, 2012-10-21 13:41:50.937
1536, 0010154108, 2012-10-21 13:41:57.968
1537, 0010154108, 2012-10-21 13:42:03.953
1538, 0010154108, 2012-10-21 13:42:11.93
1539, 0010154108, 2012-10-21 13:42:17.953
1540, 0010154108, 2012-10-21 13:42:23.968
1541, 0010154108, 2012-10-21 13:42:30.187
1542, 0010154108, 2012-10-21 13:42:37.968
1543, 0010154108, 2012-10-21 13:42:44
1544, 0010154108, 2012-10-21 13:42:50.15
1545, 0010154108, 2012-10-21 13:42:56
1546, 0010154108, 2012-10-21 13:43:03.31
1547, 0010154108, 2012-10-21 13:43:08.984
1548, 0010154108, 2012-10-21 13:43:14.984
1549, 0010154108, 2012-10-21 13:43:21.281
1550, 0010154108, 2012-10-21 13:43:28.62
1551, 0010154108, 2012-10-21 13:43:35
1552, 0010154108, 2012-10-21 13:43:41.78
1553, 0010154108, 2012-10-21 13:43:48.78
1554, 0010154108, 2012-10-21 13:43:54
1555, 0010154108, 2012-10-21 13:43:59.968
1556, 0010154108, 2012-10-21 13:44:06.62
1557, 0010154108, 2012-10-21 13:44:13.15
1558, 0010154108, 2012-10-21 13:44:19.968
1559, 0010154108, 2012-10-21 13:44:25.984
1560, 0010154108, 2012-10-21 13:44:33.15
1561, 0010154108, 2012-10-21 13:44:39.78
1562, 0010154108, 2012-10-21 13:44:45.46
1563, 0010154108, 2012-10-21 13:44:53.78
1564, 0010154108, 2012-10-21 13:44:59.78
1565, 0010154108, 2012-10-21 13:45:05.437
1566, 0010154108, 2012-10-21 13:45:12.953
1567, 0010154108, 2012-10-21 13:45:19.625
1568, 0010154108, 2012-10-21 13:45:25.937
1569, 0010154108, 2012-10-21 13:45:32.921
1570, 0010154108, 2012-10-21 13:45:38.937
1571, 0010154108, 2012-10-21 13:45:44.890
1572, 0010154108, 2012-10-21 13:45:50.843
1573, 0010154108, 2012-10-21 13:45:57.843
1574, 0010154108, 2012-10-21 13:46:03.843
1575, 0010154108, 2012-10-21 13:46:09.906
1576, 0010154108, 2012-10-21 13:46:15.906
1577, 0010154108, 2012-10-21 13:46:22.921
1578, 0010154108, 2012-10-21 13:46:28.859
1579, 0010154108, 2012-10-21 13:46:34.796
1580, 0010154108, 2012-10-21 13:46:40.703
1581, 0010154108, 2012-10-21 13:46:48.656
1582, 0010154108, 2012-10-21 13:46:54.687
1583, 0010154108, 2012-10-21 13:47:00.671
1584, 0010154108, 2012-10-21 13:47:07.687
1585, 0010154108, 2012-10-21 13:47:13.750
1586, 0010154108, 2012-10-21 13:47:20.265
1587, 0010154108, 2012-10-21 13:47:27.812
1588, 0010154108, 2012-10-21 13:47:33.796
1589, 0010154108, 2012-10-21 13:47:39.781
1590, 0010154108, 2012-10-21 13:47:45.765
1591, 0010154108, 2012-10-21 13:47:52.734
1592, 0010154108, 2012-10-21 13:47:58.781
1593, 0010154108, 2012-10-21 13:48:04.812
1594, 0010154108, 2012-10-21 13:48:10.843
1595, 0010154108, 2012-10-21 13:48:17.843
1596, 0010154108, 2012-10-21 13:48:23.843
1597, 0010154108, 2012-10-21 13:48:29.859
1598, 0010154108, 2012-10-21 13:48:35.890
1599, 0010154108, 2012-10-21 13:48:42.906
1600, 0010154108, 2012-10-21 13:48:48.875
1601, 0010154108, 2012-10-21 13:48:54.890
1602, 0010154108, 2012-10-21 13:49:00.890
1603, 0010154108, 2012-10-21 13:49:07.843
1604, 0010154108, 2012-10-21 13:49:13.890
1605, 0010154108, 2012-10-21 13:49:19.890
1606, 0010154108, 2012-10-21 13:49:28.468
1607, 0010154108, 2012-10-21 13:49:34.500
1608, 0010154108, 2012-10-21 13:49:40.484
1609, 0010154108, 2012-10-21 13:49:46.453
1610, 0010154108, 2012-10-21 13:49:52.859
1611, 0010154108, 2012-10-21 13:49:58.984
1612, 0010154108, 2012-10-21 13:50:04.875
1613, 0010154108, 2012-10-21 13:50:10.875
1614, 0010154108, 2012-10-21 13:50:17.796
1615, 0010154108, 2012-10-21 13:50:23.812
1616, 0010154108, 2012-10-21 13:50:29.796
1617, 0010154108, 2012-10-21 13:50:35.812
1618, 0010154108, 2012-10-21 13:50:42.812
1619, 0010154108, 2012-10-21 13:50:48.828
1620, 0010154108, 2012-10-21 13:50:54.906
1621, 0, 2012-10-21 13:51:00.828
1622, 0, 2012-10-21 13:51:07.890
1623, 0, 2012-10-21 13:51:13.796
1624, 0, 2012-10-21 13:51:19.796
1625, 0, 2012-10-21 13:51:25.812
1626, 0, 2012-10-21 13:51:32.828
1627, 0, 2012-10-21 13:51:38.828
1628, 0, 2012-10-21 13:51:44.796
1629, 0, 2012-10-21 13:51:50.781
1630, 0, 2012-10-21 13:51:57.750
1631, 0, 2012-10-21 13:52:03.718
1632, 0, 2012-10-21 13:52:12.718
1633, 0, 2012-10-21 13:52:18.765
1634, 0, 2012-10-21 13:52:25.46
1635, 0, 2012-10-21 13:52:32.796
1636, 0, 2012-10-21 13:52:39.765
1637, 0, 2012-10-21 13:52:45.781
1638, 0, 2012-10-21 13:52:52.812
1639, 0, 2012-10-21 13:52:59.218
1640, 0, 2012-10-21 13:53:05.250
1641, 0, 2012-10-21 13:53:12.765
1642, 0, 2012-10-21 13:53:18.781
1643, 0, 2012-10-21 13:53:24.875
1644, 0, 2012-10-21 13:53:30.890
1645, 0, 2012-10-21 13:53:37.765
1646, 0, 2012-10-21 13:53:43.750
1647, 0, 2012-10-21 13:53:49.734
1648, 0, 2012-10-21 13:53:55.718
1649, 0, 2012-10-21 13:54:02.859
1650, 0, 2012-10-21 13:54:08.750
1651, 0, 2012-10-21 13:54:14.781
1652, 0, 2012-10-21 13:54:20.750
1653, 0, 2012-10-21 13:54:27.703
1654, 0, 2012-10-21 13:54:33.656
1655, 0, 2012-10-21 13:54:39.671
1656, 0, 2012-10-21 13:54:46.671
1657, 0, 2012-10-21 13:54:53.671
1658, 0, 2012-10-21 13:54:59.656
1659, 0, 2012-10-21 13:55:05.656
1660, 0, 2012-10-21 13:55:12.640
1661, 0, 2012-10-21 13:55:18.640
1662, 0, 2012-10-21 13:55:24.656
1663, 0, 2012-10-21 13:55:30.718
1664, 0, 2012-10-21 13:55:37.703
1665, 0, 2012-10-21 13:55:43.703
1666, 0, 2012-10-21 13:55:49.671
1667, 0, 2012-10-21 13:55:55.609
1668, 0, 2012-10-21 13:56:02.609
1669, 0, 2012-10-21 13:56:08.625
1670, 0, 2012-10-21 13:56:14.625
1671, 0, 2012-10-21 13:56:20.593
1672, 0, 2012-10-21 13:56:27.625
1673, 0, 2012-10-21 13:56:33.687
1674, 0, 2012-10-21 13:56:39.640
1675, 0, 2012-10-21 13:56:45.578
1676, 0, 2012-10-21 13:56:53.703
1677, 0, 2012-10-21 13:56:59.984
1678, 0, 2012-10-21 13:57:07.562
1679, 0, 2012-10-21 13:57:14.562
1680, 0, 2012-10-21 13:57:20.578
1681, 0, 2012-10-21 13:57:27.625
1682, 0, 2012-10-21 13:57:33.609
1683, 0, 2012-10-21 13:57:39.546
1684, 0, 2012-10-21 13:57:47.609
1685, 0, 2012-10-21 13:57:55.765
1686, 0, 2012-10-21 13:58:02.453
1687, 0, 2012-10-21 13:58:08.609
1688, 0, 2012-10-21 13:58:14.484
1689, 0, 2012-10-21 13:58:22.500
1690, 0, 2012-10-21 13:58:28.703
1691, 0, 2012-10-21 13:58:35
1692, 0, 2012-10-21 13:58:42.656
1693, 0, 2012-10-21 13:58:49.468
1694, 0, 2012-10-21 13:58:55.453
1695, 0, 2012-10-21 13:59:02.453
1696, 0, 2012-10-21 13:59:08.453
1697, 0, 2012-10-21 13:59:14.453
1698, 0, 2012-10-21 13:59:20.453
1699, 0, 2012-10-21 13:59:27.453
1700, 0, 2012-10-21 13:59:33.484
1701, 0, 2012-10-21 13:59:40.515
1702, 0, 2012-10-21 13:59:47.515
1703, 0, 2012-10-21 13:59:53.531
1704, 0, 2012-10-21 14:00:01.437
1705, 0, 2012-10-21 14:00:07.484
1706, 0, 2012-10-21 14:00:13.484
1707, 0, 2012-10-21 14:00:19.500
1708, 0, 2012-10-21 14:00:25.531
1709, 0, 2012-10-21 14:00:32.562
1710, 0, 2012-10-21 14:00:38.531
1711, 0, 2012-10-21 14:00:44.515
1712, 0, 2012-10-21 14:00:50.484
1713, 0, 2012-10-21 14:00:57.531
1714, 0, 2012-10-21 14:01:04.62
1715, 0, 2012-10-21 14:01:09.968
1716, 0, 2012-10-21 14:01:17.531
1717, 0, 2012-10-21 14:01:23.546
1718, 0, 2012-10-21 14:01:29.546
1719, 0, 2012-10-21 14:01:35.531
1720, 0, 2012-10-21 14:01:42.546
1721, 0, 2012-10-21 14:01:48.562
1722, 0, 2012-10-21 14:01:54.531
1723, 0, 2012-10-21 14:02:00.546
1724, 0, 2012-10-21 14:02:07.671
1725, 0, 2012-10-21 14:02:13.468
1726, 0, 2012-10-21 14:02:19.921
1727, 0, 2012-10-21 14:02:27.468
1728, 0, 2012-10-21 14:02:33.484
1729, 0, 2012-10-21 14:02:39.468
1730, 0, 2012-10-21 14:02:45.484
1731, 0, 2012-10-21 14:02:52.484
1732, 0, 2012-10-21 14:02:58.484
1733, 0, 2012-10-21 14:03:04.515
1734, 0, 2012-10-21 14:03:12.500
1735, 0, 2012-10-21 14:03:18.421
1736, 0, 2012-10-21 14:03:24.437
1737, 0, 2012-10-21 14:03:30.734
1738, 0, 2012-10-21 14:03:37.484
1739, 0, 2012-10-21 14:03:43.437
1740, 0, 2012-10-21 14:03:49.437
1741, 0, 2012-10-21 14:03:55.484
1742, 0, 2012-10-21 14:04:02.484
1743, 0, 2012-10-21 14:04:08.453
1744, 0, 2012-10-21 14:04:14.531
1745, 0, 2012-10-21 14:04:20.500
1746, 0, 2012-10-21 14:04:27.484
1747, 0, 2012-10-21 14:04:33.515
1748, 0, 2012-10-21 14:04:39.484
1749, 0, 2012-10-21 14:04:45.375
1750, 0, 2012-10-21 14:04:52.375
1751, 0, 2012-10-21 14:04:58.406
1752, 0, 2012-10-21 14:05:04.421
1753, 0, 2012-10-21 14:05:10.437
1754, 0, 2012-10-21 14:05:17.406
1755, 0, 2012-10-21 14:05:23.421
1756, 0, 2012-10-21 14:05:29.453
1757, 0, 2012-10-21 14:05:35.453
1758, 0, 2012-10-21 14:05:42.468
1759, 0, 2012-10-21 14:05:48.437
1760, 0, 2012-10-21 14:05:54.437
1761, 0, 2012-10-21 14:06:00.453
1762, 0, 2012-10-21 14:06:07.406
1763, 0, 2012-10-21 14:06:13.390
1764, 0, 2012-10-21 14:06:19.343
1765, 0, 2012-10-21 14:06:25.359
1766, 0, 2012-10-21 14:06:32.343
1767, 0, 2012-10-21 14:06:38.343
1768, 0, 2012-10-21 14:06:44.359
1769, 0, 2012-10-21 14:06:50.343
1770, 0, 2012-10-21 14:06:57.343
1771, 0, 2012-10-21 14:07:03.328
1772, 0, 2012-10-21 14:07:09.312
1773, 0, 2012-10-21 14:07:15.343
1774, 0, 2012-10-21 14:07:22.812
1775, 0, 2012-10-21 14:07:29.312
1776, 0, 2012-10-21 14:07:35.328
1777, 0, 2012-10-21 14:07:42.343
1778, 0, 2012-10-21 14:07:48.296
1779, 0, 2012-10-21 14:07:54.343
1780, 0, 2012-10-21 14:08:00.343
1781, 0, 2012-10-21 14:08:07.328
1782, 0, 2012-10-21 14:08:13.312
1783, 0, 2012-10-21 14:08:19.265
1784, 0, 2012-10-21 14:08:26.46
1785, 0, 2012-10-21 14:08:32.296
1786, 0, 2012-10-21 14:08:38.296
1787, 0, 2012-10-21 14:08:44.281
1788, 0, 2012-10-21 14:08:50.296
1789, 0, 2012-10-21 14:08:57.265
1790, 0, 2012-10-21 14:09:03.828
1791, 0, 2012-10-21 14:09:09.937
1792, 0, 2012-10-21 14:09:17.265
1793, 0, 2012-10-21 14:09:23.546
1794, 0, 2012-10-21 14:09:30.468
1795, 0, 2012-10-21 14:09:39.406
1796, 0, 2012-10-21 14:09:49.453
1797, 0, 2012-10-21 14:09:57.562
1798, 0, 2012-10-21 14:10:04.359
1799, 0, 2012-10-21 14:10:15.609
1800, 0, 2012-10-21 14:10:22.187
1801, 0, 2012-10-21 14:10:28.218
1802, 0, 2012-10-21 14:10:34.218
1803, 0, 2012-10-21 14:10:40.296
1804, 0, 2012-10-21 14:10:50.250
1805, 0, 2012-10-21 14:10:57.265
1806, 0, 2012-10-21 14:11:03.125
1807, 0, 2012-10-21 14:11:09.125
1808, 0, 2012-10-21 14:11:15.125
1809, 0, 2012-10-21 14:11:22.125
1810, 0, 2012-10-21 14:11:28.109
1811, 0, 2012-10-21 14:11:35.390
1812, 0, 2012-10-21 14:11:42.15
1813, 0, 2012-10-21 14:11:48.31
1814, 0, 2012-10-21 14:11:54.46
1815, 0, 2012-10-21 14:12:00.78
1816, 0, 2012-10-21 14:12:08.140
1817, 0, 2012-10-21 14:12:14.140
1818, 0, 2012-10-21 14:12:20.171
1819, 0, 2012-10-21 14:12:27.218
1820, 0, 2012-10-21 14:12:33.171
1821, 0, 2012-10-21 14:12:39.125
1822, 0, 2012-10-21 14:12:46
1823, 0, 2012-10-21 14:12:52.93
1824, 0, 2012-10-21 14:12:58.593
1825, 0, 2012-10-21 14:13:05.484
1826, 0, 2012-10-21 14:13:12.78
1827, 0, 2012-10-21 14:13:18.93
1828, 0, 2012-10-21 14:13:24.109
1829, 0, 2012-10-21 14:13:30.140
1830, 0, 2012-10-21 14:13:37.140
1831, 0, 2012-10-21 14:13:43.203
1832, 0, 2012-10-21 14:13:49.281
1833, 0, 2012-10-21 14:13:57.296
1834, 0, 2012-10-21 14:14:03.203
1835, 0, 2012-10-21 14:14:09.671
1836, 0, 2012-10-21 14:14:17.265
1837, 0, 2012-10-21 14:14:23.171
1838, 0, 2012-10-21 14:14:29.187
1839, 0, 2012-10-21 14:14:35.109
1840, 0, 2012-10-21 14:14:43.484
1841, 0, 2012-10-21 14:14:49.515
1842, 0, 2012-10-21 14:14:57.203
1843, 0, 2012-10-21 14:15:03.187
1844, 0, 2012-10-21 14:15:09.125
1845, 0, 2012-10-21 14:15:17.156
1846, 0, 2012-10-21 14:15:23.140
1847, 0, 2012-10-21 14:15:29.109
1848, 0, 2012-10-21 14:15:35.109
1849, 0, 2012-10-21 14:15:42.125
1850, 0, 2012-10-21 14:15:48.93
1851, 0, 2012-10-21 14:15:54.125
1852, 0, 2012-10-21 14:16:00.109
1853, 0, 2012-10-21 14:16:07.546
1854, 0, 2012-10-21 14:16:14.78
1855, 0, 2012-10-21 14:16:20.156
1856, 0, 2012-10-21 14:16:28.93
1857, 0, 2012-10-21 14:16:34.546
1858, 0, 2012-10-21 14:16:42.62
1859, 0, 2012-10-21 14:16:50.437
1860, 0, 2012-10-21 14:17:13.15
1861, 0, 2012-10-21 14:17:20.593
1862, 0, 2012-10-21 14:17:28.125
1863, 0, 2012-10-21 14:17:35.578
1864, 0, 2012-10-21 14:17:43.125
1865, 0, 2012-10-21 14:17:52.484
Upvotes: 1
Views: 489
Reputation: 49049
I based the answer on the following table structure, that Sir Rufo provided in another answer:
CREATE TABLE `userstamp` (
`user_key` int(11) NOT NULL ,
`id` int(11) NOT NULL AUTO_INCREMENT ,
`stamptime` datetime NOT NULL ,
PRIMARY KEY (`id`)
);
This VIEW shows every "active" interval, for every user:
CREATE VIEW Intervals AS
SELECT
userstamp.user_key,
userstamp.stamptime as checkin,
min(userstamp_2.stamptime) as checkout
FROM
userstamp inner join userstamp userstamp_2
on userstamp.user_key = userstamp_2.user_key
WHERE
userstamp_2.id > userstamp.id
AND Mod(
(SELECT
Count(*)
FROM
userstamp as userstamp_1
WHERE
userstamp.user_key = userstamp_1.user_key
AND userstamp.id>userstamp_1.id)
, 2 ) = 0
GROUP BY
userstamp.user_key, userstamp.id
the weird SELECT subquery in the WHERE clause is there to get rid of "inactive" intervals (like 16:00 - 16:05) by counting if the checkout time for that user is in an odd row or not.
Now we have to deal with the fact that an active interval could start in one day, and end in the next day. This is also the right place to convert dates to seconds. I would use this VIEW, based on an UNION query:
CREATE VIEW intervals_2 AS
SELECT
user_key,
CAST(checkin AS DATE) AS day,
TIME_TO_SEC(CAST(checkin AS TIME)) AS checkinsec,
TIME_TO_SEC(CAST(checkout AS TIME)) AS checkoutsec
FROM intervals WHERE CAST(checkin AS date) = CAST(checkout AS date)
UNION
SELECT
user_key,
CAST(checkin AS date) AS day,
TIME_TO_SEC(CAST(checkin AS TIME)) AS checkinsec,
86400 AS checkoutsec
FROM intervals WHERE CAST(checkin AS date) < CAST(checkout AS date)
UNION
SELECT
user_key,
CAST(checkout AS DATE) AS day,
0 AS checkinsec,
TIME_TO_SEC(CAST(checkout AS TIME)) AS checkoutsec
FROM intervals WHERE CAST(checkin AS date) < CAST(checkout AS date)
Okay! Now we are ready to do some basic sum. This one is to calculate the sum of each day:
SELECT
user_key,
day,
SEC_TO_TIME( SUM(IF(checkinsec<82800 and checkoutsec>21600,LEAST(82800, checkoutsec) - GREATEST(21600, checkinsec),0))) as WorkHours,
SEC_TO_TIME( SUM(IF(checkinsec<21600, 21600-checkinsec, 0)+IF(checkoutsec>82800, checkoutsec-GREATEST(82800, checkinsec), 0))) as NightShift,
SEC_TO_TIME( SUM(checkoutsec-checkinsec) ) as TotalDay
FROM
intervals_2
WHERE
day BETWEEN startdate AND enddate
GROUP BY
user_key,
day
but if you just need the sum of the whole week you can remove the DATE(checkin) from the field and from the GROUP BY.
Upvotes: 2
Reputation: 19106
I have a Stored Procedure for you that can handle this and returns a table result. IMHO a normal SQL Query cannot handle this, because you have to parse through every record and toggle the states "come" and "go".
Here is the Table Layout for the SP
CREATE TABLE `userstamp` (
`user_key` int(11) NOT NULL ,
`id` int(11) NOT NULL AUTO_INCREMENT ,
`stamptime` datetime NOT NULL ,
PRIMARY KEY (`id`)
);
calling the SP looks like
CALL get_user_result( 100, '2012-09-03', '2012-09-10' );
and here is the SP
DROP PROCEDURE IF EXISTS `get_user_result`;
DELIMITER ;;
CREATE PROCEDURE `get_user_result`(IN Auser_key INT, IN AStartDate DATE, IN AEndDate DATE)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE LKnownUser INT DEFAULT 0;
DECLARE LCstampdate DATE;
DECLARE LCuser_key INT;
DECLARE Lday_shift_inc, Lnight_shift_inc DECIMAL( 9, 6 ) DEFAULT 0; -- CHANGE
DECLARE LCstamptime, Lstamptime DATETIME;
DECLARE c_ustamp CURSOR FOR SELECT user_key, stamptime FROM userstamp WHERE user_key = Auser_key AND stamptime >= ADDDATE( AStartDate, INTERVAL 6 HOUR ) AND stamptime < ADDDATE( DATE_ADD( AEndDate, INTERVAL 6 HOUR ), 1 ) ORDER BY user_key,stamptime;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
DROP TEMPORARY TABLE IF EXISTS user_result;
CREATE TEMPORARY TABLE user_result (
`stampdate` date NULL DEFAULT NULL ,
`user_key` int(11) NOT NULL ,
`day_shift` decimal(9,6) NOT NULL DEFAULT 0.00 , -- CHANGE
`night_shift` decimal(9,6) NOT NULL DEFAULT 0.00 , -- CHANGE
`stamptime` datetime NULL DEFAULT NULL ,
PRIMARY KEY (`stampdate`,`user_key`)
);
OPEN c_ustamp;
REPEAT
FETCH c_ustamp INTO LCuser_key, LCstamptime;
IF NOT done THEN
IF ( TIME( LCstamptime ) >= MAKETIME(00,00,00) ) AND ( TIME( LCstamptime ) < MAKETIME(06,00,00) ) THEN
SET LCstampdate = DATE_SUB( DATE( LCstamptime ), INTERVAL 1 DAY );
ELSE
SET LCstampdate = DATE( LCstamptime );
END IF;
SELECT COUNT(*)
INTO LKnownUser
FROM user_result
WHERE stampdate = LCstampdate AND user_key = LCuser_key;
IF NOT LKnownUser THEN
INSERT INTO user_result
SET stampdate = LCstampdate, user_key = LCuser_key, stamptime = LCstamptime, day_shift = 0, night_shift = 0;
ELSE
SELECT stamptime
INTO Lstamptime
FROM user_result
WHERE stampdate = LCstampdate AND user_key = LCuser_key;
IF Lstamptime IS NULL THEN
UPDATE user_result
SET stamptime = LCstamptime
WHERE stampdate = LCstampdate AND user_key = LCuser_key;
ELSE
IF ( TIME( Lstamptime ) >= MAKETIME(06,00,00) ) AND ( TIME( Lstamptime ) < MAKETIME(23,00,00) )
THEN -- day_shift 06:00:00 til 23:00:00
SET Lday_shift_inc = TIME_TO_SEC( TIMEDIFF( LCstamptime, Lstamptime ) ) / 3600.0; -- CHANGE
SET Lnight_shift_inc = 0;
ELSE -- night_shift
SET Lday_shift_inc = 0;
SET Lnight_shift_inc = TIME_TO_SEC( TIMEDIFF( LCstamptime, Lstamptime ) ) / 3600.0; -- CHANGE
END IF;
UPDATE user_result
SET
day_shift = day_shift + Lday_shift_inc,
night_shift = night_shift + Lnight_shift_inc,
stamptime = NULL
WHERE stampdate = LCstampdate AND user_key = LCuser_key;
END IF;
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE c_ustamp;
SELECT stampdate, user_key, day_shift, night_shift
FROM user_result
WHERE day_shift + night_shift > 0;
DROP TEMPORARY TABLE IF EXISTS user_result;
END;;
DELIMITER ;
Upvotes: 0
Reputation: 6247
Let me recommend these things:
Upvotes: 2