- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这是我需要解决的问题:
这是我需要为每个用户获取的表格。
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
所以让我解释一下。在此示例中,用户选择的星期是 3.9-10.9,用户具有唯一键。如果可能的话,这个表中的所有内容都需要在 sql 中生成。所以第一列是日期,第二列是(06:00-23:00)之间的工作时间,第三列是(23:00-06:00)之间的夜类,第四列是白天和晚上的总计移位,第五列仅在第四列大于 8 时才填充,即总数的 9h - 8 为 1,仅当底部的(日类和夜类)总数大于 48h 时才填充第六列,然后将差异写入最后一列。
我当前的数据表有以下列:user_key,id,time
现在这将是查询应该做什么的示例:
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)
所以我们 user_key 对于每个用户都是唯一的并且 id 是自动递增的,我们总是需要为同一个用户获取第一条和第二条记录并保存结果,然后为该用户获取第二条记录并节省工作时间等等...如何做到这一点?我需要使用一些sql变量吗?当然如果是白类和夜类就必须要有选择,因为我们不想把工作时间浪费在错误的栏目上。
编辑:
据我所知,你们中的许多人并不了解我真正需要的是什么。我将尝试对其进行更多解释。
当用户登录时,它会写入自己的 user_key。当他注销时,我在表中写了 user_key(0) 之后另一个用户可以唱歌等等。所以基本上我需要在例如白类中查看第一个唯一用户 key 并获取最后一个。我们可以获得的最后一个 user_key 只是为了检查下一个 user_key 是否不同。如果下一个用户 key 不同,那么我们有第一个和最后一个,它们之间的差异需要保存在变量中。然后例如我们有一些其他用户,稍后我们又有一对 user_key 然后我们只需要将它们之间的差异添加到同一个变量等等......
我的问题也可以用 PHP 解决。例如,我们只能获取特定 user_key 和特定时间的值。然后在 php 中我们可以遍历数据并检查。
请提供一些例子
编辑:
数据:
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
最佳答案
我的答案基于下表结构,Rufo 爵士在另一个答案中提供:
CREATE TABLE `userstamp` (
`user_key` int(11) NOT NULL ,
`id` int(11) NOT NULL AUTO_INCREMENT ,
`stamptime` datetime NOT NULL ,
PRIMARY KEY (`id`)
);
此 View 显示每个用户的每个“事件”间隔:
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
WHERE 子句中奇怪的 SELECT 子查询用于通过计算该用户的结帐时间是否在奇数行中来摆脱“非事件”间隔(如 16:00 - 16:05)。
现在我们必须处理一个事件间隔可能在一天开始并在第二天结束的事实。这也是将日期转换为秒的正确位置。我会使用这个基于 UNION 查询的 View :
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)
好的!现在我们准备做一些基本的求和。这个是计算每一天的总和:
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
但是如果您只需要整周的总和,您可以从该字段和 GROUP BY 中删除 DATE(checkin)。
关于mysql - 如何形成 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12932993/
SQL、PL-SQL 和 T-SQL 之间有什么区别? 谁能解释一下这三者之间的区别,并提供每一个的相关使用场景? 最佳答案 SQL 是一种对集合进行操作的查询语言。 它或多或少是标准化的,几乎所有关
这个问题已经有答案了: What is the difference between SQL, PL-SQL and T-SQL? (6 个回答) 已关闭 9 年前。 我对 SQL 的了解足以完成我的
我在数据库中有一个 USER 表。该表有一个 RegistrationDate 列,该列有一个默认约束为 GETDATE()。 使用 LINQ 时,我没有为 RegistrationDate 列提供任
我有一个可能属于以下类型的字符串 string expected result 15-th-rp 15 15/12-rp 12 15-12-th
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 9年前关闭
我有一个存储过程(称为 sprocGetArticles),它从文章表中返回文章列表。这个存储过程没有任何参数。 用户可以对每篇文章发表评论,我将这些评论存储在由文章 ID 链接的评论表中。 有什么方
我目前正在做一个 *cough*Oracle*cough* 数据库主题。讲师介绍embedded SQL作为让其他语言(例如 C、C++)与(Oracle)数据库交互的方式。 我自己做了一些数据库工作
SQL Server 中 SQL 语句的最大长度是多少?这个长度是否取决于 SQL Server 的版本? 例如,在 DECLARE @SQLStatement NVARCHAR(MAX) = N'S
这个问题已经有答案了: Simple way to transpose columns and rows in SQL? (9 个回答) 已关闭 8 年前。 CallType
预先感谢您对此提供的任何帮助。 假设我有一个查询,可以比较跨年的数据,从某个任意年份开始,永无止境(进入 future ),每年同一时期直到最后一个完整的月份(其特点是一月数据永远不会显示至 2 月
我在数据库中有一个 USER 表。该表有一个 RegistrationDate 列,该列的默认约束为 GETDATE()。 使用 LINQ 时,我没有为 RegistrationDate 列提供任何数
下面是我试图用来检查存储过程是否不存在然后创建过程的 sql。它会抛出一个错误:Incorrect syntax near the keyword 'PROCEDURE' IF NOT EXISTS
我有一个同事声称动态 SQL 在许多情况下比静态 SQL 执行得更快,所以我经常看到 DSQL 到处都是。除了明显的缺点,比如在运行之前无法检测到错误并且更难阅读,这是否准确?当我问他为什么一直使用
来自 lobodava 的动态 SQL 查询是: declare @sql nvarchar(4000) = N';with cteColumnts (ORDINAL_POSITION, CO
使用 SQL Server 中的存储过程执行动态 SQL 命令的现实优点和缺点是什么 EXEC (@SQL) 对比 EXEC SP_EXECUTESQL @SQL ? 最佳答案 sp_executes
我有这个有效的 SQL 查询: select sum(dbos.Points) as Points, dboseasons.Year from dbo.StatLines dbos i
我正在调试一些构建成功运行的 SQL 命令的代码。 然而,在查询结束时,查询结果似乎被写入了一个文本文件。 完整的查询如下 echo SELECT DATE,DATETABLE,DATE,APPDAT
我有一些创建表的 .sql 文件(MS SQL 数据库): 表_1.sql: IF OBJECT_ID (N'my_schema.table1', N'U') IS NOT NULL DROP TAB
我写了下面的 SQL 存储过程,它一直给我错误@pid = SELECT MAX(... 整个过程是: Alter PROCEDURE insert_partyco @pname varchar(20
我在 SQL Server 2005 中有包含两列 Fruit 和 Color 的表,如下所示 Fruit Colour Apple Red Orange
我是一名优秀的程序员,十分优秀!