- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个工作时间注册器,我想知道是否有人有一个简单的解决方案来从时间戳数组中选择“夜间时间”,它代表工作的开始和结束(当然这可能是一夜之间)。
示例时间戳:
SELECT start, end FROM work
start end
2016-05-23 18:13:59 2016-05-24 02:12:45
2016-05-24 18:12:47 2016-05-25 02:13:39
2016-05-25 17:39:28 2016-05-26 01:37:35
2016-05-26 17:39:42 2016-05-27 01:39:31
2016-05-30 01:59:43 2016-05-30 10:41:37
2016-05-31 01:55:23 2016-05-31 10:49:11
2016-06-01 02:01:21 2016-06-01 12:03:13
我想返回总时间(在一个字段中),这是在 22:00:00
之后和 06:00:00
之前 - 所以夜间时间。有任何想法吗?谢谢。
最佳答案
这假设您在问题下的评论中提供的信息 maximum set of night hours 始终为 1。希望这会简化一些事情。
下面对其进行了轻微扩展。它允许 worker 在每个类次的 2 个日历日 block 中打多达 3 个夜间时间 block 。这些由 3 个范围表示。如果轮类的总工作时间超过 2 个日历日,则可以轻松扩展它。但是请注意,示例数据的最后一行是 41 小时的长类次。只问是否需要如何扩展它。但相同的数据显示了各种边界条件测试,希望现在涵盖所有内容。
create table times
( id int auto_increment primary key,
startDT datetime not null,
endDT datetime not null
);
insert times(startDT,endDT) values
('2016-05-23 18:13:59','2016-05-24 02:12:45'),
('2016-05-24 18:12:47','2016-05-25 02:13:39'),
('2016-05-25 17:39:28','2016-05-26 01:37:35'),
('2016-05-26 17:39:42','2016-05-27 01:39:31'),
('2016-05-30 01:59:43','2016-05-30 10:41:37'),
('2016-05-31 01:55:23','2016-05-31 10:49:11'),
('2016-06-01 02:01:21','2016-06-01 12:03:13'),
('2016-06-01 05:30:00','2016-06-01 13:00:00'),
('2016-06-01 05:30:00','2016-06-01 22:35:00'),
('2016-06-01 05:30:00','2016-06-01 22:30:00'),
('2016-06-01 05:30:00','2016-06-02 22:30:00');
select id,startDt,endDt,
coalesce(@r1Begin:=concat(date(startDt),' 00:00:00'),null) as `@r1Begin`,
coalesce(@r1End:=concat(date(startDt),' 06:00:00'),null) as `@r1End`,
coalesce(@r2Begin:=concat(date(startDt),' 22:00:00'),null) as `@r2Begin`,
coalesce(@r2End:=concat(date(date_add(date(startDt),interval 1 day)), ' 06:00:00'),null) as `@r2End`,
coalesce(@r3Begin:=concat(date(date_add(date(startDt),interval 1 day)), ' 22:00:00'),null) as `@r3Begin`,
coalesce(@r3End:=concat(date(date_add(date(startDt),interval 1 day)), ' 23:59:59'),null) as `@r3End`,
(secondsOverlapped(startDt,endDt,@r1Begin,@r1End) + secondsOverlapped(startDt,endDt,@r2Begin,@r2End) + secondsOverlapped(startDt,endDt,@r3Begin,@r3End) ) / 3600 as graveyardHours
from times;
上面放大到只有 4 列
+----+---------------------+---------------------+----------------+
| id | startDt | endDt | graveyardHours |
+----+---------------------+---------------------+----------------+
| 1 | 2016-05-23 18:13:59 | 2016-05-24 02:12:45 | 4.2125 |
| 2 | 2016-05-24 18:12:47 | 2016-05-25 02:13:39 | 4.2275 |
| 3 | 2016-05-25 17:39:28 | 2016-05-26 01:37:35 | 3.6264 |
| 4 | 2016-05-26 17:39:42 | 2016-05-27 01:39:31 | 3.6586 |
| 5 | 2016-05-30 01:59:43 | 2016-05-30 10:41:37 | 4.0047 |
| 6 | 2016-05-31 01:55:23 | 2016-05-31 10:49:11 | 4.0769 |
| 7 | 2016-06-01 02:01:21 | 2016-06-01 12:03:13 | 3.9775 |
| 8 | 2016-06-01 05:30:00 | 2016-06-01 13:00:00 | 0.5000 |
| 9 | 2016-06-01 05:30:00 | 2016-06-01 22:35:00 | 1.0833 |
| 10 | 2016-06-01 05:30:00 | 2016-06-01 22:30:00 | 1.0000 |
| 11 | 2016-06-01 05:30:00 | 2016-06-02 22:30:00 | 9.0000 |
+----+---------------------+---------------------+----------------+
id 11 是一个 41 小时轮类,涉及 3 个灰场小时段,但只占 9 个灰场小时:1/2 小时 + 8 小时 + 1/2 小时
select sum(graveyardShiftHoursWorked) as graveYardHours
from
( select id,startDt,endDt,
coalesce(@r1Begin:=concat(date(startDt),' 00:00:00'),null) as `@r1Begin`,
coalesce(@r1End:=concat(date(startDt),' 06:00:00'),null) as `@r1End`,
coalesce(@r2Begin:=concat(date(startDt),' 22:00:00'),null) as `@r2Begin`,
coalesce(@r2End:=concat(date(date_add(date(startDt),interval 1 day)), ' 06:00:00'),null) as `@r2End`,
coalesce(@r3Begin:=concat(date(date_add(date(startDt),interval 1 day)), ' 22:00:00'),null) as `@r3Begin`,
coalesce(@r3End:=concat(date(date_add(date(startDt),interval 1 day)), ' 23:59:59'),null) as `@r3End`,
(secondsOverlapped(startDt,endDt,@r1Begin,@r1End) + secondsOverlapped(startDt,endDt,@r2Begin,@r2End) + secondsOverlapped(startDt,endDt,@r3Begin,@r3End) ) / 3600 as graveyardShiftHoursWorked
from times
) xDerived;
+----------------+
| graveyardHours |
+----------------+
| 39.3674 |
+----------------+
以下是一个用户定义的函数,它采用工作日期时间开始和结束,并确定与提供的范围重叠的秒数以将其与之进行比较。我把它作为练习留给读者去完善它,以便解决所有错误陷阱。例如,如果提供的工作日范围有工作人员的结束 datetime
发生在开始 datetime
之前(即:您的数据有误),等等。
请注意,该函数返回 seconds
。在查询本身中,使用此函数除以 3600
以转换为小时。
drop function if exists secondsOverlapped;
DELIMITER $$
create function secondsOverlapped(r1Begin datetime,r1End datetime,r2Begin datetime,r2End datetime)
RETURNS int DETERMINISTIC
BEGIN
DECLARE beginOverride datetime;
DECLARE endOverride datetime;
DECLARE elapsedSeconds int;
IF (r1End<=r2Begin) or (r2End<=r1Begin) THEN
return 0;
END IF;
set beginOverride=greatest(r1Begin,r2Begin);
set endOverride=least(r1End,r2End);
set elapsedSeconds=TIME_TO_SEC(TIMEDIFF(endOverride,beginOverride));
return elapsedSeconds;
END$$
DELIMITER ;
CREATE PROCEDURE and CREATE FUNCTION 的 Mysql 手册页和最小和最大 functions
关于mysql - 返回时间大于 22 :00 and smaller than 06:00 for group of different timestamps 的 MySql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37568734/
我想编写一个 linq 表达式,该表达式将返回不包含特定值的 ID。例如,我想返回所有不具有 Value = 30 的不同 ID。 ID, Value 1, 10 1, 20 1, 30 2,
我正在尝试使用 Regexp 匹配 Nmap 命令的输出。可以有两种不同的格式。 第一种格式(当 nmap 可以找到主机名时) Nmap scan report for 2u4n32t-n4 (192
我正在 Visual Studio 2012 上使用 C# 开发一个软件。我使用 MySQL Connector 6.9.1 进行 MySQL 连接。我的软件在我的操作系统(Win8 x64)上运行顺
在 Django 中(使用 django.contrib.auth 时)我可以添加一个 Group到另一个 Group ?即一个Group成为另一个成员(member) Group ? 如果是这样,我
我试图通过使用动态组参数对数据进行分组来循环。 我们可以在循环的 WHERE 条件上使用动态查询,但我不知道是否可以在组条件中使用动态字符串。 以下是用户决定按哪个字段分组,然后根据决定放置其他逻辑的
我有这样的字符串 s = 'MR1|L2-S1x' 模式总是相同的:一个或两个字符,在 [|.+:x-] 中可选地后跟一个数字和一个分隔符。此模式可以重复 6 次。 所以匹配模式很明确。 p = r'
我有一个带有时间戳字段“bar”的表“foo”。如何仅获取查询的最旧时间戳,例如: SELECT foo.bar from foo?我尝试执行以下操作: SELECT MIN(foo.bar) fro
在我的 Django 项目中,我有一个 user_manage 应用程序。 我在 user_manage 应用的 model.py 中创建了一个名为 UserManage 的模型: from djan
所以我有这样的输入: 还有一个模板指令,例如: 看来我只获得了 foo 和 bar 的组。 (为什么?我预计我可能会得到第三组 current-group-key() = '')。
我正在尝试扩展 django.contrib.auth 并遇到将用户添加到组中的情况,这可以通过两种方式完成。我只是想知道为什么会这样,以及其中一种相对于另一种的优势是什么。 最佳答案 他们做完全相同
我使用的是旧的 PHP 脚本,并且此查询有错误。由于我没有使用 mysql 的经验,因此无法修复它。 "SELECT COUNT(p.postid) AS pid, p.*, t.* FROM ".T
我有几行 Objective-C 代码,例如: ABAddressBookRef addressBook; CFErrorRef error = NULL; addressBook = ABAddre
我正在使用 MariaDB IMDB 电影数据集,我试图解决以下问题。电影表包含 id、名称、排名和年份列 A decade is a sequence of 10 consecutive years
让我从数据开始,以便更好地描述我的需求。我有一个名为 SUPERMARKET 的表,其中包含以下字段: Field 1: StoreID Field 2: ProductCategory Field
你好我有这个查询: SELECT DISTINCT a.id, a.runcd, (SELECT SUM(b.CALVAL) FROM GRS b WHERE b.PCode=11000 AND a.
我想在 xquery 中使用 Group By。有人可以告诉我如何在 Marklogic 中使用 Group By 吗? 最佳答案 或者,您可以使用 xdmp:xslt-invoke 调用 XSLT或
因此,当通过 from sequelize 请求组时,如下所示: return models.WorkingCalendar .findAll({
我希望我解释正确。 我有 2 个表,有 第一个表(table1) +------------+------+-------+-------+ | Date | Item | Block |
我的表 MYTABLE 有 2 列:A 和 B 我有以下代码片段: SELECT MYTABLE.A FROM MYTABLE HAVING SUM(MYTABLE.B) > 100
我有一个简单的行分组查询,需要 0.0045 秒。 300.000 行 从表 GROUP BY cid 中选择 cid 当我添加 MAX() 进行查询时,需要 0.65 秒才能返回。 从表 GROUP
我是一名优秀的程序员,十分优秀!